-
Notifications
You must be signed in to change notification settings - Fork 683
Expand file tree
/
Copy pathNodeTestPlugin.ts
More file actions
157 lines (131 loc) · 4.58 KB
/
NodeTestPlugin.ts
File metadata and controls
157 lines (131 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import child_process from 'node:child_process';
import path from 'node:path';
import { FileSystem, JsonFile } from '@rushstack/node-core-library';
import type {
HeftConfiguration,
IHeftTaskPlugin,
IHeftTaskRunHookOptions,
IHeftTaskSession
} from '@rushstack/heft';
/** @alpha */
export interface INodeTestPluginOptions {
testPattern?: string | undefined;
}
interface INodeTestConfiguration {
testPattern?: string;
}
const PLUGIN_NAME: 'node-test-plugin' = 'node-test-plugin';
const UPDATE_SNAPSHOTS_PARAMETER_LONG_NAME: '--update-snapshots' = '--update-snapshots';
const DEFAULT_TEST_PATTERN: string = 'test/**/*.test.mjs';
const CONFIG_FILE_NAME: string = 'node-test.json';
/**
* @internal
*/
export default class NodeTestPlugin implements IHeftTaskPlugin<INodeTestPluginOptions> {
private _updateSnapshots: boolean = false;
public apply(
taskSession: IHeftTaskSession,
heftConfiguration: HeftConfiguration,
options: INodeTestPluginOptions = {}
): void {
this._updateSnapshots = taskSession.parameters.getFlagParameter(
UPDATE_SNAPSHOTS_PARAMETER_LONG_NAME
).value;
taskSession.hooks.run.tapPromise(PLUGIN_NAME, async (runOptions: IHeftTaskRunHookOptions) => {
await this._runNodeTestAsync(taskSession, heftConfiguration, options, runOptions.abortSignal);
});
}
private async _loadConfigurationAsync(
heftConfiguration: HeftConfiguration,
options: INodeTestPluginOptions
): Promise<INodeTestConfiguration> {
// Try to load configuration from config/node-test.json
const configPath: string = path.join(
heftConfiguration.buildFolderPath,
'config',
CONFIG_FILE_NAME
);
let config: INodeTestConfiguration = {};
if (await FileSystem.existsAsync(configPath)) {
config = await JsonFile.loadAsync(configPath);
}
// Options passed via heft.json take precedence
if (options.testPattern) {
config.testPattern = options.testPattern;
}
return config;
}
private async _runNodeTestAsync(
taskSession: IHeftTaskSession,
heftConfiguration: HeftConfiguration,
options: INodeTestPluginOptions,
abortSignal: AbortSignal
): Promise<void> {
const config: INodeTestConfiguration = await this._loadConfigurationAsync(
heftConfiguration,
options
);
const testPattern: string = config.testPattern || DEFAULT_TEST_PATTERN;
taskSession.logger.terminal.writeLine(`Running Node.js tests with pattern: ${testPattern}`);
if (this._updateSnapshots) {
taskSession.logger.terminal.writeLine('Updating snapshots...');
}
await this._executeNodeTest(
heftConfiguration.buildFolderPath,
testPattern,
this._updateSnapshots,
abortSignal,
taskSession
);
}
private async _executeNodeTest(
buildFolderPath: string,
testPattern: string,
updateSnapshots: boolean,
abortSignal: AbortSignal,
taskSession: IHeftTaskSession
): Promise<void> {
return new Promise<void>((resolve, reject) => {
// Build the node test runner arguments
const args: string[] = ['--test', '--test-reporter', 'spec'];
if (updateSnapshots) {
args.push('--test-update-snapshots');
}
// Add the test pattern
args.push(testPattern);
taskSession.logger.terminal.writeDebugLine(`Executing: node ${args.join(' ')}`);
const child = child_process.spawn('node', args, {
stdio: ['inherit', 'inherit', 'inherit'],
cwd: buildFolderPath,
env: process.env
});
// Handle abort signal
const abortHandler = (): void => {
child.kill('SIGTERM');
};
if (abortSignal.aborted) {
child.kill('SIGTERM');
reject(new Error('Operation aborted'));
return;
}
abortSignal.addEventListener('abort', abortHandler);
child.on('error', (error) => {
abortSignal.removeEventListener('abort', abortHandler);
reject(new Error(`Failed to spawn Node.js test process: ${error.message}`));
});
child.on('exit', (code, signal) => {
abortSignal.removeEventListener('abort', abortHandler);
if (signal === 'SIGTERM' && abortSignal.aborted) {
reject(new Error('Operation aborted'));
} else if (code === 0) {
taskSession.logger.terminal.writeLine('Tests completed successfully.');
resolve();
} else {
reject(new Error(`Node.js test process exited with code ${code}`));
}
});
});
}
}