UNPKG

3.06 kBJavaScriptView Raw
1/* eslint-disable jest/no-jest-import */
2/* eslint-disable global-require */
3/* eslint-disable import/no-dynamic-require */
4// https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/utils/createJestConfig.js
5const path = require('path');
6
7const jest = require('jest');
8const { existsSync } = require('fs');
9
10function create(settings) {
11 settings.init();
12 const rootDir = settings.project();
13 const jestInitExists = existsSync(`${path.join(settings.app(), 'jest.init.js')}`);
14
15 const setupFilesPath = path.join(settings.project(), 'jest.setup.js');
16 const setupFilesExist = existsSync(setupFilesPath);
17
18 const setupFiles = setupFilesExist ? [`${require.resolve(setupFilesPath)}`] : [];
19
20 // Allow developers to add their own node_modules include path
21 const userInclude = settings.configuration.development.babelInclude;
22 const includes = ['@av', ...userInclude].join('|');
23
24 const config = {
25 collectCoverageFrom: ['project/app/**/*.{js,jsx,ts,tsx}'],
26 coveragePathIgnorePatterns: ['/node_modules/', '/coverage/', '/dist/'],
27 testEnvironment: 'node',
28 testURL: 'http://localhost',
29 transform: {
30 // Jest and Babel don't allow functions in the options so we just return their values here
31 '^.+\\.(js|jsx|ts|tsx)$': [`${require.resolve('./jest/babel.js')}`,{
32 isProduction: settings.isProduction(),
33 isTesting: settings.isTesting(),
34 isDevelopment: settings.isDevelopment(),
35 isDistribution: settings.isDistribution(),
36 targets: settings.targets(),
37 }],
38 '^.+\\.css$': `${require.resolve('./jest/css.js')}`,
39 '^(?!.*\\.(js|jsx|ts|tsx|css|json)$)': `${require.resolve('./jest/file.js')}`
40 },
41 setupFiles: [require.resolve('raf/polyfill'), ...setupFiles],
42 setupFilesAfterEnv: jestInitExists
43 ? require(path.join(settings.app(), 'jest.init.js'))
44 : ['@testing-library/jest-dom/extend-expect'],
45 transformIgnorePatterns: [`[/\\\\]node_modules[/\\\\](?!(${includes})).+\\.(js|jsx|ts|tsx)$`],
46 testMatch: [
47 // Ignore the following directories:
48 // build
49 // - the build output directory
50 // .cache
51 // - the yarn module cache on Ubuntu if $HOME === rootDir
52 // docs
53 // - often used to publish to Github Pages
54 // node_modules
55 // - ignore tests in dependencies
56 // dist
57 // - the dist output directory
58 '<rootDir>/!(build|docs|dist|node_modules|scripts)/**/__tests__/**/*.(js|ts|tsx)?(x)',
59 '<rootDir>/!(build|docs|dist|node_modules|scripts)/**/?(*.)(spec|test).(js|ts|tsx)?(x)'
60 ],
61 globals: settings.globals()
62 };
63
64 if (rootDir) {
65 config.rootDir = rootDir;
66 }
67
68 return config;
69}
70
71function unit(settings) {
72 const argv = process.argv.slice(2);
73 const jestConfig = JSON.stringify(create(settings));
74 argv.push(`--config=${jestConfig}`);
75 argv.push('--env=jsdom');
76
77 jest.run(argv);
78
79 return Promise.resolve();
80}
81
82module.exports = settings => ({
83 run: () => unit(settings),
84 description: 'Run your tests using Jest'
85});