@nest-yalc-2/jest-config 1.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +661 -0
- package/README.md +31 -0
- package/package.json +31 -0
- package/src/CHANGELOG.md +8 -0
- package/src/README.md +31 -0
- package/src/config/CHANGELOG.md +8 -0
- package/src/config/README.md +31 -0
- package/src/config/gql-plugin.js +25 -0
- package/src/config/index.d.ts +2 -0
- package/src/config/index.js +19 -0
- package/src/config/index.js.map +1 -0
- package/src/config/index.ts +2 -0
- package/src/config/jest-conf.generator.d.ts +53 -0
- package/src/config/jest-conf.generator.js +228 -0
- package/src/config/jest-conf.generator.js.map +1 -0
- package/src/config/jest-conf.generator.ts +368 -0
- package/src/config/jest-def.config.d.ts +20 -0
- package/src/config/jest-def.config.js +172 -0
- package/src/config/jest-def.config.js.map +1 -0
- package/src/config/jest-def.config.ts +210 -0
- package/src/config/jest.setup.d.ts +0 -0
- package/src/config/jest.setup.js +9 -0
- package/src/config/jest.setup.js.map +1 -0
- package/src/config/jest.setup.ts +39 -0
- package/src/config/package.json +22 -0
- package/src/config/tsconfig.dev.json +3 -0
- package/src/config/tsconfig.json +9 -0
- package/src/gql-plugin.js +25 -0
- package/src/index.d.ts +2 -0
- package/src/index.js +19 -0
- package/src/index.js.map +1 -0
- package/src/index.ts +2 -0
- package/src/jest-conf.generator.d.ts +53 -0
- package/src/jest-conf.generator.js +227 -0
- package/src/jest-conf.generator.js.map +1 -0
- package/src/jest-conf.generator.ts +362 -0
- package/src/jest-def.config.d.ts +20 -0
- package/src/jest-def.config.js +187 -0
- package/src/jest-def.config.js.map +1 -0
- package/src/jest-def.config.ts +225 -0
- package/src/jest.setup.d.ts +0 -0
- package/src/jest.setup.js +25 -0
- package/src/jest.setup.js.map +1 -0
- package/src/jest.setup.ts +58 -0
- package/src/package.json +22 -0
- package/src/tsconfig.dev.json +3 -0
- package/src/tsconfig.json +9 -0
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
/* istanbul ignore file */
|
|
2
|
+
|
|
3
|
+
import * as path from 'path';
|
|
4
|
+
import * as readTsConfig from 'get-tsconfig';
|
|
5
|
+
import { pathsToModuleNameMapper } from 'ts-jest';
|
|
6
|
+
import { defaults } from 'jest-config';
|
|
7
|
+
import type { JestConfigWithTsJest } from 'ts-jest';
|
|
8
|
+
|
|
9
|
+
export const coveragePathIgnorePatterns = [
|
|
10
|
+
'/env/dist/',
|
|
11
|
+
'/node_modules/',
|
|
12
|
+
'/database/seeds/',
|
|
13
|
+
'/database/migrations/',
|
|
14
|
+
'/test/feature/',
|
|
15
|
+
];
|
|
16
|
+
|
|
17
|
+
export const globals = () => {
|
|
18
|
+
return {
|
|
19
|
+
__JEST_DISABLE_DB: true,
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export const tsJestConfigE2E = (
|
|
24
|
+
tsConfPath = '',
|
|
25
|
+
withGqlPlugin = true,
|
|
26
|
+
overrideTsJestConfig?: any,
|
|
27
|
+
) => {
|
|
28
|
+
const tsConfigFile = readTsConfig.getTsconfig(path.resolve(tsConfPath));
|
|
29
|
+
const { tsconfig, ...restTsJest } = overrideTsJestConfig ?? {};
|
|
30
|
+
|
|
31
|
+
const conf: any = {
|
|
32
|
+
diagnostics: false,
|
|
33
|
+
tsconfig: {
|
|
34
|
+
...(tsConfigFile?.config.compilerOptions ?? {}),
|
|
35
|
+
...(tsconfig ?? {}),
|
|
36
|
+
},
|
|
37
|
+
...restTsJest,
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
if (withGqlPlugin) {
|
|
41
|
+
conf.astTransformers = {
|
|
42
|
+
before: [path.join(__dirname, 'gql-plugin.js')],
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return conf;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export const tsJestConfig = (tsConfPath = '', overrideTsJestConfig?: any) => {
|
|
50
|
+
const tsConfigFile = readTsConfig.getTsconfig(
|
|
51
|
+
path.resolve(path.dirname(tsConfPath)),
|
|
52
|
+
path.basename(tsConfPath),
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
if (tsConfigFile?.path && path.resolve(tsConfigFile.path) !== path.resolve(tsConfPath)) {
|
|
56
|
+
throw new Error(`Cannot find ${tsConfPath}`);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const { tsconfig, ...restTsJest } = overrideTsJestConfig ?? {};
|
|
60
|
+
|
|
61
|
+
const config = {
|
|
62
|
+
tsconfig: {
|
|
63
|
+
...(tsConfigFile?.config.compilerOptions ?? {}),
|
|
64
|
+
...(tsconfig ?? {}),
|
|
65
|
+
},
|
|
66
|
+
diagnostics: false,
|
|
67
|
+
...restTsJest,
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
return config;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
export const coverageThreshold = (
|
|
74
|
+
projects: any[] = [],
|
|
75
|
+
defaultCoverageThreshold = {
|
|
76
|
+
branches: 100,
|
|
77
|
+
functions: 100,
|
|
78
|
+
lines: 100,
|
|
79
|
+
statements: 100,
|
|
80
|
+
},
|
|
81
|
+
) => {
|
|
82
|
+
const coverage: Record<string, any> = {
|
|
83
|
+
global: {
|
|
84
|
+
...defaultCoverageThreshold,
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
projects.map((project) => {
|
|
89
|
+
// /**
|
|
90
|
+
// * @todo should be removed and be set to 100% asap
|
|
91
|
+
// */
|
|
92
|
+
// switch (project.name) {
|
|
93
|
+
// case 'unit/common/crud-gen':
|
|
94
|
+
// branches = 92.27;
|
|
95
|
+
// functions = 98.92;
|
|
96
|
+
// lines = 98.75;
|
|
97
|
+
// statements = 98.5;
|
|
98
|
+
// break;
|
|
99
|
+
// default:
|
|
100
|
+
// branches = 100;
|
|
101
|
+
// break;
|
|
102
|
+
// }
|
|
103
|
+
|
|
104
|
+
coverage[project.rootDir] = {
|
|
105
|
+
...defaultCoverageThreshold,
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
if (project?.coverageThreshold) {
|
|
109
|
+
coverage[project.rootDir] = {
|
|
110
|
+
...coverage[project.rootDir],
|
|
111
|
+
...project?.coverageThreshold,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
return coverage;
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
export interface IDefaultConfOptions {
|
|
120
|
+
/** node modules to transform
|
|
121
|
+
* string array -> add extra esmodule to transform
|
|
122
|
+
* true -> transform esmodule only using the default array
|
|
123
|
+
* false -> disable esmodule transformation (default)
|
|
124
|
+
*/
|
|
125
|
+
transformEsModules?: string[] | boolean;
|
|
126
|
+
tsJestConf?: any;
|
|
127
|
+
jestConf?: any;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* for both unit and e2e
|
|
132
|
+
* @param dirname
|
|
133
|
+
* @returns
|
|
134
|
+
*/
|
|
135
|
+
const defaultConf = (
|
|
136
|
+
dirname: string,
|
|
137
|
+
options: IDefaultConfOptions = {},
|
|
138
|
+
_tsJestConfig: any = {},
|
|
139
|
+
): JestConfigWithTsJest => {
|
|
140
|
+
const tsConfigFile = readTsConfig.getTsconfig(
|
|
141
|
+
path.join(dirname, 'tsconfig.json'),
|
|
142
|
+
);
|
|
143
|
+
const compilerOptions = tsConfigFile?.config.compilerOptions ?? {};
|
|
144
|
+
|
|
145
|
+
// We need this to make sure that some esm modules are transformed
|
|
146
|
+
// ref: https://github.com/nrwl/nx/issues/812
|
|
147
|
+
// ref: https://github.com/jaredpalmer/tsdx/issues/187#issuecomment-825536863
|
|
148
|
+
|
|
149
|
+
const config: JestConfigWithTsJest = {
|
|
150
|
+
rootDir: dirname,
|
|
151
|
+
modulePathIgnorePatterns: [
|
|
152
|
+
'<rootDir>/var/',
|
|
153
|
+
'<rootDir>/env/',
|
|
154
|
+
'<rootDir>/docs/',
|
|
155
|
+
'<rootDir>/dist/',
|
|
156
|
+
'<rootDir>/../dist/',
|
|
157
|
+
'<rootDir>/node_modules/',
|
|
158
|
+
'.*/dist/',
|
|
159
|
+
],
|
|
160
|
+
preset: 'ts-jest/presets/default-esm',
|
|
161
|
+
testEnvironment: 'node',
|
|
162
|
+
moduleFileExtensions: [...defaults.moduleFileExtensions, 'ts'], // add typescript to the default options
|
|
163
|
+
testRegex: '.*\\.spec\\.ts$',
|
|
164
|
+
transform: {
|
|
165
|
+
'^.+\\.(t|j)sx?$': [
|
|
166
|
+
'@swc/jest',
|
|
167
|
+
{
|
|
168
|
+
jsc: {
|
|
169
|
+
target: 'es2022',
|
|
170
|
+
parser: {
|
|
171
|
+
syntax: 'typescript',
|
|
172
|
+
decorators: true,
|
|
173
|
+
dynamicImport: true,
|
|
174
|
+
},
|
|
175
|
+
transform: {
|
|
176
|
+
legacyDecorator: true,
|
|
177
|
+
decoratorMetadata: true,
|
|
178
|
+
},
|
|
179
|
+
keepClassNames: true,
|
|
180
|
+
},
|
|
181
|
+
module: {
|
|
182
|
+
type: 'es6',
|
|
183
|
+
},
|
|
184
|
+
},
|
|
185
|
+
],
|
|
186
|
+
},
|
|
187
|
+
moduleNameMapper: {
|
|
188
|
+
// this fixes the issue with wrong line numbers in stack traces,
|
|
189
|
+
/** @see https://github.com/kulshekhar/ts-jest/issues/727 */
|
|
190
|
+
'source-map-support/register': 'identity-obj-proxy',
|
|
191
|
+
// for ESM support
|
|
192
|
+
'^(\\.{1,2}/.*)\\.js$': '$1.ts',
|
|
193
|
+
...pathsToModuleNameMapper(compilerOptions.paths ?? {}, {
|
|
194
|
+
prefix: `${dirname}/`,
|
|
195
|
+
useESM: true,
|
|
196
|
+
}),
|
|
197
|
+
},
|
|
198
|
+
errorOnDeprecated: true,
|
|
199
|
+
extensionsToTreatAsEsm: ['.ts'],
|
|
200
|
+
// injectGlobals: false,
|
|
201
|
+
...options?.jestConf,
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
if (options.transformEsModules) {
|
|
205
|
+
const esModules = [
|
|
206
|
+
...(Array.isArray(options.transformEsModules)
|
|
207
|
+
? options.transformEsModules
|
|
208
|
+
: []),
|
|
209
|
+
'aggregate-error',
|
|
210
|
+
'clean-stack',
|
|
211
|
+
'escape-string-regexp',
|
|
212
|
+
'indent-string',
|
|
213
|
+
'p-map',
|
|
214
|
+
'lodash-es',
|
|
215
|
+
].join('|');
|
|
216
|
+
|
|
217
|
+
config.transformIgnorePatterns = [
|
|
218
|
+
`[/\\\\]node_modules[/\\\\](?!${esModules}).+\\.(js|jsx)$`,
|
|
219
|
+
];
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
return config;
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
export default defaultConf;
|
|
File without changes
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
process.env.NODE_ENV = 'test';
|
|
3
|
+
if (!process.env.LISTENING_TO_UNHANDLED_REJECTION) {
|
|
4
|
+
process.on('unhandledRejection', (err) => {
|
|
5
|
+
throw err;
|
|
6
|
+
});
|
|
7
|
+
process.env.LISTENING_TO_UNHANDLED_REJECTION = 'true';
|
|
8
|
+
}
|
|
9
|
+
jest.mock('@nestjs/graphql', () => {
|
|
10
|
+
const actual = jest.requireActual('@nestjs/graphql');
|
|
11
|
+
const mockedModule = Object.assign({}, actual);
|
|
12
|
+
const decorators = [
|
|
13
|
+
'Args', 'Field', 'Query', 'Mutation', 'Resolver', 'InputType', 'ObjectType',
|
|
14
|
+
'InterfaceType', 'ResolveField', 'HideField', 'Directive', 'registerEnumType', 'IntersectionType'
|
|
15
|
+
];
|
|
16
|
+
for (const name of decorators) {
|
|
17
|
+
if (actual[name]) {
|
|
18
|
+
mockedModule[name] = jest.fn().mockImplementation((...args) => {
|
|
19
|
+
return actual[name](...args);
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return mockedModule;
|
|
24
|
+
});
|
|
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@@
|
|
|
1
|
+
/// <reference types="jest" />
|
|
2
|
+
/* istanbul ignore file */
|
|
3
|
+
|
|
4
|
+
process.env.NODE_ENV = 'test';
|
|
5
|
+
|
|
6
|
+
// without this, jest won't fail with unhandled promises
|
|
7
|
+
// (e.g. missing awaits)
|
|
8
|
+
if (!process.env.LISTENING_TO_UNHANDLED_REJECTION) {
|
|
9
|
+
process.on('unhandledRejection', (err) => {
|
|
10
|
+
throw err;
|
|
11
|
+
});
|
|
12
|
+
// Avoid memory leak by adding too many listeners
|
|
13
|
+
process.env.LISTENING_TO_UNHANDLED_REJECTION = 'true';
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// try {
|
|
17
|
+
// jest.unstable_mockModule('@nestjs/graphql', () => {
|
|
18
|
+
// class Fake {}
|
|
19
|
+
// return {
|
|
20
|
+
// ...jest.requireActual<any>('@nestjs/graphql'),
|
|
21
|
+
// InputType: () => jest.fn(),
|
|
22
|
+
// OmitType: () => Fake,
|
|
23
|
+
// PickType: () => Fake,
|
|
24
|
+
// PartialType: () => Fake,
|
|
25
|
+
// IntersectionType: () => Fake,
|
|
26
|
+
// };
|
|
27
|
+
// });
|
|
28
|
+
// // eslint-disable-next-line no-console
|
|
29
|
+
// console.debug('Mocked @nestjs/graphql');
|
|
30
|
+
// } catch (e) {
|
|
31
|
+
// // eslint-disable-next-line no-console
|
|
32
|
+
// console.debug("Can't mock @nestjs/graphql. Not installed?");
|
|
33
|
+
// }
|
|
34
|
+
|
|
35
|
+
// jest.mock('@nestjs/common', () => {
|
|
36
|
+
// return {
|
|
37
|
+
// ...jest.requireActual<any>('@nestjs/common'),
|
|
38
|
+
// Injectable: () => jest.fn(),
|
|
39
|
+
// };
|
|
40
|
+
// });
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
jest.mock('@nestjs/graphql', () => {
|
|
44
|
+
const actual = jest.requireActual('@nestjs/graphql');
|
|
45
|
+
const mockedModule = Object.assign({}, actual);
|
|
46
|
+
const decorators = [
|
|
47
|
+
'Args', 'Field', 'Query', 'Mutation', 'Resolver', 'InputType', 'ObjectType',
|
|
48
|
+
'InterfaceType', 'ResolveField', 'HideField', 'Directive', 'registerEnumType', 'IntersectionType'
|
|
49
|
+
];
|
|
50
|
+
for (const name of decorators) {
|
|
51
|
+
if (actual[name]) {
|
|
52
|
+
mockedModule[name] = jest.fn().mockImplementation((...args) => {
|
|
53
|
+
return actual[name](...args);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return mockedModule;
|
|
58
|
+
});
|
package/src/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nest-yalc-2/jest-config",
|
|
3
|
+
"version": "1.3.3",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "./index.ts",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./index.ts",
|
|
8
|
+
"./*": "./*.ts"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
12
|
+
},
|
|
13
|
+
"author": "",
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"types": "./index.ts",
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"tslib": "^2.6.3"
|
|
21
|
+
}
|
|
22
|
+
}
|