1 | import * as fs from "fs-extra";
|
2 | import {spawnAsync} from "../commons/spawnAsync";
|
3 | import * as path from "path";
|
4 | import {IProjectConfig} from "../commons/IProjectConfig";
|
5 | import {Logger} from "@simplism/core";
|
6 |
|
7 | export async function testAsync(argv: { config?: string; package?: string }): Promise<void> {
|
8 | process.env.NODE_ENV = "development";
|
9 |
|
10 | let configFilePath = argv.config;
|
11 | configFilePath = configFilePath ? path.resolve(process.cwd(), configFilePath)
|
12 | : fs.existsSync(path.resolve(process.cwd(), "simplism.ts")) ? path.resolve(process.cwd(), "simplism.ts")
|
13 | : fs.existsSync(path.resolve(process.cwd(), "simplism.js")) ? path.resolve(process.cwd(), "simplism.js")
|
14 | : path.resolve(process.cwd(), "simplism.json");
|
15 |
|
16 | if (path.extname(configFilePath) === ".ts") {
|
17 |
|
18 | require("ts-node/register");
|
19 | }
|
20 |
|
21 |
|
22 | const projectConfig = eval("require(configFilePath)") as IProjectConfig;
|
23 |
|
24 | if (!projectConfig.tests) {
|
25 | throw new Error(`${argv.config}에 테스트 설정이 없습니다.`);
|
26 | }
|
27 |
|
28 | const loadersPath = (...args: string[]): string => {
|
29 | return fs.existsSync(path.resolve(process.cwd(), "node_modules/@simplism/cli/loaders"))
|
30 | ? path.resolve(process.cwd(), "node_modules/@simplism/cli/loaders", ...args)
|
31 | : path.resolve(__dirname, "../../loaders", ...args);
|
32 | };
|
33 |
|
34 | const runAsync = async (packageName: string) => {
|
35 | const logger = new Logger("@simplism/cli", `TEST ${packageName}:`);
|
36 | logger.log(`테스트`);
|
37 |
|
38 | const testConfig = projectConfig.tests!.single(item => item.name === packageName);
|
39 | if (!testConfig) {
|
40 | throw new Error(`테스트 패키지 ${packageName}에 대한 테스트 설정이 없습니다.`);
|
41 | }
|
42 |
|
43 | const includeArgs = testConfig.packages.mapMany(item => [`--include`, `packages/${item}/src/**`]);
|
44 |
|
45 | const shimRequireArg = testConfig.angular ? [`--require`, `"${loadersPath("test-angular.shim.js")}"`] :
|
46 | testConfig.jsdom ? [`--require`, `"${loadersPath("test-jsdom.shim.js")}"`]
|
47 | : [];
|
48 |
|
49 | const requireOrWebpackConfig = testConfig.angular ? [
|
50 | "--webpack-config", `"${loadersPath("test-angular-webpack.config.js")}"`,
|
51 | "--webpack-env.packageName", `"${packageName}"`
|
52 | ]
|
53 | : [
|
54 | `--require`, `ts-node/register`,
|
55 | `--require`, `tsconfig-paths/register`
|
56 | ];
|
57 |
|
58 | await spawnAsync(
|
59 | logger,
|
60 | [
|
61 | `nyc`,
|
62 | `--all`,
|
63 | `--report-dir`, `coverage/${packageName}`,
|
64 | `--temp-directory`, `.nyc_output/${packageName}`,
|
65 | ...includeArgs,
|
66 | `--reporter`, `html`,
|
67 | `--extension`, `.ts`,
|
68 |
|
69 | testConfig.angular ? `mocha-webpack` : `mocha`,
|
70 | ...requireOrWebpackConfig,
|
71 | ...shimRequireArg,
|
72 | `--bail`,
|
73 | `--timeout`, `10000`,
|
74 | `tests/${packageName}/**/*.spec.ts`
|
75 | ],
|
76 | {
|
77 | cwd: process.cwd(),
|
78 | env: {
|
79 | ...process.env,
|
80 | TS_NODE_PROJECT: `tests/${packageName}/tsconfig.json`,
|
81 | NODE_ENV: "test",
|
82 | JSDOM_CONFIG: JSON.stringify(testConfig.jsdom || testConfig.angular)
|
83 | }
|
84 | }
|
85 | );
|
86 | logger.log(`테스트 완료`);
|
87 | };
|
88 |
|
89 | if (!argv.package) {
|
90 | fs.removeSync(path.resolve(process.cwd(), ".nyc_output"));
|
91 | fs.removeSync(path.resolve(process.cwd(), "coverage"));
|
92 |
|
93 | const promiseList: Promise<void>[] = [];
|
94 | for (const testConfig of projectConfig.tests) {
|
95 | promiseList.push(runAsync(testConfig.name));
|
96 | }
|
97 | await Promise.all(promiseList);
|
98 | }
|
99 | else {
|
100 | for (const pack of argv.package.split(",")) {
|
101 | fs.removeSync(path.resolve(process.cwd(), ".nyc_output", pack.trim()));
|
102 | fs.removeSync(path.resolve(process.cwd(), "coverage", pack.trim()));
|
103 |
|
104 | await runAsync(pack.trim());
|
105 | }
|
106 | }
|
107 | } |
\ | No newline at end of file |