1 |
|
2 |
|
3 |
|
4 |
|
5 | const path = require('path');
|
6 |
|
7 | const jest = require('jest');
|
8 | const { existsSync } = require('fs');
|
9 |
|
10 | function 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 |
|
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 |
|
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 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
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 |
|
71 | function 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 |
|
82 | module.exports = settings => ({
|
83 | run: () => unit(settings),
|
84 | description: 'Run your tests using Jest'
|
85 | });
|