1 |
|
2 |
|
3 |
|
4 |
|
5 | const path = require('path');
|
6 |
|
7 |
|
8 | require('jest/node_modules/jest-cli/build/cli');
|
9 |
|
10 | const jest = require('jest');
|
11 | const { existsSync } = require('fs');
|
12 |
|
13 | function create(settings) {
|
14 | settings.init();
|
15 | const rootDir = settings.project();
|
16 | const jestInitExists = existsSync(`${path.join(settings.app(), 'jest.init.js')}`);
|
17 |
|
18 | const setupFilesPath = path.join(settings.project(), 'jest.setup.js');
|
19 | const setupFilesExist = existsSync(setupFilesPath);
|
20 |
|
21 | const setupFiles = setupFilesExist ? [`${require.resolve(setupFilesPath)}`] : [];
|
22 |
|
23 |
|
24 | const userInclude = settings.configuration.development.babelInclude;
|
25 | const includes = ['@av', ...userInclude].join('|');
|
26 |
|
27 | const config = {
|
28 | collectCoverageFrom: ['project/app/**/*.{js,jsx}'],
|
29 | coveragePathIgnorePatterns: ['/node_modules/', '/coverage/', '/dist/'],
|
30 | testEnvironment: 'node',
|
31 | testURL: 'http://localhost',
|
32 | transform: {
|
33 | '^.+\\.(js|jsx)$': `${require.resolve('./jest/babel.js')}`,
|
34 | '^.+\\.css$': `${require.resolve('./jest/css.js')}`,
|
35 | '^(?!.*\\.(js|jsx|css|json)$)': `${require.resolve('./jest/file.js')}`
|
36 | },
|
37 | setupFiles: [require.resolve('raf/polyfill'), ...setupFiles],
|
38 | setupFilesAfterEnv: jestInitExists
|
39 | ? require(path.join(settings.app(), 'jest.init.js'))
|
40 | : ['jest-dom/extend-expect', '@testing-library/react/cleanup-after-each'],
|
41 | transformIgnorePatterns: [`[/\\\\]node_modules[/\\\\](?!(${includes})).+\\.(js|jsx)$`],
|
42 | testMatch: [
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | '<rootDir>/!(build|docs|dist|node_modules|scripts)/**/__tests__/**/*.js?(x)',
|
55 | '<rootDir>/!(build|docs|dist|node_modules|scripts)/**/?(*.)(spec|test).js?(x)'
|
56 | ],
|
57 | globals: settings.globals()
|
58 | };
|
59 |
|
60 | if (rootDir) {
|
61 | config.rootDir = rootDir;
|
62 | }
|
63 |
|
64 | return config;
|
65 | }
|
66 |
|
67 | function unit(settings) {
|
68 | const argv = process.argv.slice(2);
|
69 | const jestConfig = JSON.stringify(create(settings));
|
70 | argv.push(`--config=${jestConfig}`);
|
71 | argv.push('--env=jsdom');
|
72 |
|
73 | jest.run(argv);
|
74 |
|
75 | return Promise.resolve();
|
76 | }
|
77 |
|
78 | module.exports = settings => ({
|
79 | run: () => unit(settings),
|
80 | description: 'Run your tests using Jest'
|
81 | });
|