Skip to content
This repository was archived by the owner on Aug 12, 2020. It is now read-only.

Commit 7de9523

Browse files
committed
Jest now can grok Typescript
* Coverage does something, but sadly the coverage reported incorrectly. * Test has been converted to ts Fixes #15
1 parent f7a779a commit 7de9523

File tree

4 files changed

+39
-6
lines changed

4 files changed

+39
-6
lines changed

packages/react-scripts/config/jest/transform.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,38 @@
77
*/
88

99
const babelJest = require('babel-jest');
10+
const tsc = require('typescript');
1011

11-
module.exports = babelJest.createTransformer({
12+
const babelTransformer = babelJest.createTransformer({
1213
presets: [require.resolve('babel-preset-react-app')]
1314
});
15+
16+
// TODO load tsconfig.json in created app instead of duplicating tsconfig.compilerOptions here
17+
const compilerOptions = {
18+
// Overwrite module
19+
// Jest gives `SyntaxError: Unexpected token import` error when ES6 module are emitted
20+
// module: tsc.ModuleKind.ES6,
21+
module: tsc.ModuleKind.CommonJS,
22+
// Overwrite jsx
23+
// Expected Babel transformer to convert jsx to js
24+
// but Jest gives `SyntaxError: Unexpected token <` error when set to Preserve
25+
// jsx: tsc.JsxEmit.Preserve,
26+
jsx: tsc.JsxEmit.React,
27+
target: tsc.ScriptTarget.ES6,
28+
moduleResolution: tsc.ModuleResolutionKind.NodeJs,
29+
};
30+
31+
// transpile the source with TypeScript, if needed, and then with Babel
32+
module.exports = {
33+
process(src, path) {
34+
if (path.endsWith('.ts') || path.endsWith('.tsx')) {
35+
src = tsc.transpile(
36+
src,
37+
compilerOptions,
38+
path,
39+
[]
40+
);
41+
}
42+
return babelTransformer.process(src, path);
43+
},
44+
};

packages/react-scripts/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nlesc/react-scripts",
3-
"version": "0.0.9",
3+
"version": "0.0.12",
44
"description": "Configuration and scripts for Create React App with Typescript.",
55
"repository": "NLeSC/create-react-app",
66
"license": "BSD-3-Clause",
@@ -23,6 +23,7 @@
2323
"react-scripts": "./bin/react-scripts.js"
2424
},
2525
"dependencies": {
26+
"@types/jest": "^15.1.32",
2627
"autoprefixer": "6.5.1",
2728
"awesome-typescript-loader": "^2.2.4",
2829
"babel-core": "6.17.0",

packages/react-scripts/template/src/App.test.js renamed to packages/react-scripts/template/src/App.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import React from 'react';
2-
import ReactDOM from 'react-dom';
1+
import * as React from 'react';
2+
import * as ReactDOM from 'react-dom';
33
import App from './App';
44

55
it('renders without crashing', () => {

packages/react-scripts/utils/createJestConfig.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ module.exports = (resolve, rootDir, isEjecting) => {
1818
const setupTestsFile = pathExists.sync(paths.testsSetup) ? '<rootDir>/src/setupTests.js' : undefined;
1919

2020
const config = {
21-
collectCoverageFrom: ['src/**/*.{js,jsx}'],
22-
moduleFileExtensions: ['jsx', 'js', 'json'],
21+
collectCoverageFrom: ['src/**/*.{js,jsx,ts,tsx}', '!src/**/*.d.ts'],
22+
moduleFileExtensions: ['jsx', 'js', 'json', 'ts', 'tsx'],
2323
moduleNameMapper: {
2424
'^.+\\.(ico|jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': resolve('config/jest/FileStub.js'),
2525
'^.+\\.css$': resolve('config/jest/CSSStub.js')
@@ -28,6 +28,7 @@ module.exports = (resolve, rootDir, isEjecting) => {
2828
setupTestFrameworkScriptFile: setupTestsFile,
2929
testPathIgnorePatterns: ['<rootDir>/(build|docs|node_modules)/'],
3030
testEnvironment: 'node',
31+
testRegex: "(/__tests__/.*|\.(test|spec))\.(ts|tsx|js|jsx)$",
3132
};
3233
if (rootDir) {
3334
config.rootDir = rootDir;

0 commit comments

Comments
 (0)