aberlaas-test 2.10.0

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 ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) Tim Carry (tim@pixelastic.com)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,4 @@
1
+ // We make captureOutput publicly available inside of tests
2
+ import { captureOutput } from 'firost';
3
+
4
+ globalThis.captureOutput = captureOutput;
@@ -0,0 +1,4 @@
1
+ // We make dedent publicly available inside of tests
2
+ import dedent from 'dedent';
3
+
4
+ globalThis.dedent = dedent;
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Add faster to type aliases:
3
+ * - fit, ftest, fdescribe: To focus specific tests
4
+ * - xit, xtest, xdescribe: To skip specific tests
5
+ */
6
+
7
+ globalThis.fit = globalThis.it.only;
8
+ globalThis.ftest = globalThis.test.only;
9
+ globalThis.fdescribe = globalThis.describe.only;
10
+
11
+ globalThis.xit = globalThis.it.skip;
12
+ globalThis.xtest = globalThis.test.skip;
13
+ globalThis.xdescribe = globalThis.describe.skip;
@@ -0,0 +1,10 @@
1
+ // We use extended matchers from jest-extended in vitest
2
+ // It includes matcher like .toStartWith, .toBeEmpty, etc
3
+ // See: https://github.com/jest-community/jest-extended
4
+ //
5
+ // The expect.extend() from Vitest is compatible with the one from Jest, so
6
+ // setup is straightforward
7
+ import { expect } from 'vitest';
8
+ import * as matchers from 'jest-extended';
9
+
10
+ expect.extend(matchers);
@@ -0,0 +1,9 @@
1
+ /* global beforeEach, expect */
2
+ /**
3
+ * Make the variable `testName` contain the name of the current test in each
4
+ * test
5
+ */
6
+ beforeEach(() => {
7
+ const fullName = expect.getState().currentTestName;
8
+ globalThis.testName = fullName.split(' > ').pop();
9
+ });
@@ -0,0 +1,43 @@
1
+ import { dirname } from 'firost';
2
+ import { defaultExclude, defineConfig } from 'vitest/config';
3
+
4
+ const aberlaasVitestExclude = [...defaultExclude, '**/tmp/**'];
5
+
6
+ const configDir = dirname();
7
+
8
+ export default defineConfig({
9
+ test: {
10
+ // vitest default is to run in watch mode, we revert that
11
+ watch: false,
12
+ // Allow a success, even if no files are passed
13
+ passWithNoTests: true,
14
+ // Hide skipped tests, allowing less noisy debug with fit/fdescribe
15
+ hideSkippedTests: true,
16
+
17
+ // Tests should be in a __tests__ folder next to their code
18
+ include: ['**/__tests__/**/*.js?(x)'],
19
+ // We ignore temporary folders from the tests
20
+ exclude: aberlaasVitestExclude,
21
+ // Restore mocks after each tests
22
+ restoreMocks: true,
23
+
24
+ // Make describe, it, beforeEach and other globally available
25
+ globals: true,
26
+ // Run before each test file
27
+ setupFiles: [
28
+ `${configDir}/setupFiles/dedent.js`,
29
+ `${configDir}/setupFiles/captureOutput.js`,
30
+ `${configDir}/setupFiles/fit-xit-fdescribe-xdescribe.js`,
31
+ `${configDir}/setupFiles/jest-extended.js`,
32
+ `${configDir}/setupFiles/testName.js`,
33
+ ],
34
+ },
35
+ server: {
36
+ watch: {
37
+ // Vitest 2.0 uses vite watcher, so files to exclude from watching are at
38
+ // the server level
39
+ // Source: https://vitest.dev/guide/migration.html#removal-of-the-watchexclude-option
40
+ ignored: aberlaasVitestExclude,
41
+ },
42
+ },
43
+ });
package/lib/main.js ADDED
@@ -0,0 +1,132 @@
1
+ import { createVitest, registerConsoleShortcuts } from 'vitest/node';
2
+ import { env, firostError, packageRoot } from 'firost';
3
+ import { _ } from 'golgoth';
4
+ import helper from 'aberlaas-helper';
5
+ import viteConfig from '../configs/vite.js';
6
+
7
+ export default {
8
+ /**
9
+ * Test all files using Vitest
10
+ * Usage:
11
+ * $ aberlaas test # Test all files
12
+ * $ aberlaas test ./path/to/__tests__/file.js # Test specific files
13
+ * $ aberlaas test ./path/to/file.js # Test specific files
14
+ * $ aberlaas test --related # Test all related files
15
+ * $ aberlaas test --failFast # Stop early as soon as one test fails
16
+ * $ aberlaas test --flags # Flags passed to vitest
17
+ * @param {object} cliArgs CLI Argument object, as created by minimist
18
+ * @returns {boolean} true on success
19
+ */
20
+ async run(cliArgs = {}) {
21
+ const options = await this.vitestOptions(cliArgs);
22
+ const isWatchMode = !!options.watch;
23
+ const isRelatedMode = options.related?.length > 0;
24
+
25
+ // If no files are passed, we assume we want to test the current project
26
+ // We'll NOT use helper.hostRoot() as this will force going to high in the
27
+ // parent tree, where aberlaas is installed. Instead, we need to stop at the
28
+ // first level where a package.json is defined
29
+ const closestHostRoot = await packageRoot(env('INIT_CWD'));
30
+ let files = _.isEmpty(cliArgs._) ? [closestHostRoot] : cliArgs._;
31
+
32
+ // If --related is passed, the list of files will already by in the .related
33
+ // key, and need to be removed from the files
34
+ if (isRelatedMode) files = [];
35
+
36
+ // Vitest will change process.exitCode, so we save it to revert it later
37
+ const initialExitCode = process.exitCode;
38
+
39
+ const vitest = await createVitest('test', options);
40
+
41
+ // Enable keyboard interaction in watch mode
42
+ if (isWatchMode) {
43
+ registerConsoleShortcuts(vitest);
44
+ }
45
+
46
+ try {
47
+ await vitest.start(files);
48
+ } catch (err) {
49
+ // We can safely swallow the VITEST_FILES_NOT_FOUND error. It's ok to
50
+ // continue if no files are found
51
+ if (err.code != 'VITEST_FILES_NOT_FOUND') {
52
+ throw err;
53
+ }
54
+ }
55
+
56
+ const testsAreFailing = process.exitCode == 1;
57
+ process.exitCode = initialExitCode;
58
+
59
+ if (isWatchMode) {
60
+ return;
61
+ }
62
+
63
+ // Stop vitest, it doesn't stop itself by default
64
+ await vitest.close();
65
+
66
+ if (testsAreFailing) {
67
+ throw firostError('ERROR_TEST_FAIL', 'Tests are failing');
68
+ }
69
+
70
+ return true;
71
+ },
72
+
73
+ /**
74
+ * Transform all aberlaas test cli options into suitable vitest options
75
+ * @param {object} cliArgs CLI Argument object, as created by minimist
76
+ * @returns {Array} Array of options for vitest
77
+ */
78
+ async vitestOptions(cliArgs = {}) {
79
+ // Options that have special meaning in aberlaas and shouldn't be passed
80
+ // as-is to vitest
81
+ const specialMeaningCliArgs = [
82
+ '_',
83
+ 'config',
84
+ 'failFast',
85
+ 'related',
86
+ 'exclude',
87
+ ];
88
+
89
+ // Reading base options from the config file
90
+ const config = await helper.getConfig(
91
+ cliArgs.config,
92
+ 'vite.config.js',
93
+ viteConfig,
94
+ );
95
+ const optionsFromConfig = config.test;
96
+
97
+ // Enhancing options with custom CLI arguments
98
+ const optionsFromAberlaas = {
99
+ // We always allow fit/fdescribe, even in CI. Those errors will be caught
100
+ // by the lint command instead
101
+ allowOnly: true,
102
+ };
103
+ // --failFast stops early as soon as one test fails
104
+ if (cliArgs.failFast) {
105
+ optionsFromAberlaas.bail = 1;
106
+ }
107
+ // --related runs also related files
108
+ // Note (2024-10-01): The related option is not documented, but should
109
+ // contain the list of files.
110
+ if (cliArgs.related) {
111
+ optionsFromAberlaas.related = cliArgs._;
112
+ }
113
+ // --exclude arguments should be added to the existing list of exclude
114
+ // patterns
115
+ // TODO: Add test for that
116
+ if (cliArgs.exclude) {
117
+ optionsFromAberlaas.exclude = [
118
+ ...optionsFromConfig.exclude,
119
+ cliArgs.exclude,
120
+ ];
121
+ }
122
+
123
+ // Passing other CLI options directly to vitest
124
+ const optionsFromCli = _.omit(cliArgs, specialMeaningCliArgs);
125
+
126
+ return {
127
+ ...optionsFromConfig,
128
+ ...optionsFromAberlaas,
129
+ ...optionsFromCli,
130
+ };
131
+ },
132
+ };
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "aberlaas-test",
3
+ "type": "module",
4
+ "description": "aberlaas test command: Run automated tests",
5
+ "version": "2.10.0",
6
+ "repository": "pixelastic/aberlaas",
7
+ "homepage": "https://projects.pixelastic.com/aberlaas/",
8
+ "author": "Tim Carry (@pixelastic)",
9
+ "license": "MIT",
10
+ "files": [
11
+ "lib/*.js",
12
+ "configs/**/*.js"
13
+ ],
14
+ "exports": {
15
+ ".": "./lib/main.js",
16
+ "./configs/vite": "./configs/vite.js"
17
+ },
18
+ "main": "./lib/main.js",
19
+ "engines": {
20
+ "node": ">=18.18.0"
21
+ },
22
+ "scripts": {
23
+ "build": "../../scripts/local/build",
24
+ "build:prod": "../../scripts/local/build-prod",
25
+ "cms": "../../scripts/local/cms",
26
+ "serve": "../../scripts/local/serve",
27
+ "ci": "../../scripts/local/ci",
28
+ "release": "../../scripts/local/release",
29
+ "update": "node ../../scripts/meta/update.js",
30
+ "test:meta": "../../scripts/local/test-meta",
31
+ "test": "../../scripts/local/test",
32
+ "test:watch": "../../scripts/local/test-watch",
33
+ "compress": "../../scripts/local/compress",
34
+ "lint": "../../scripts/local/lint",
35
+ "lint:fix": "../../scripts/local/lint-fix"
36
+ },
37
+ "dependencies": {
38
+ "aberlaas-helper": "^2.10.0",
39
+ "firost": "4.3.0",
40
+ "globals": "15.11.0",
41
+ "golgoth": "2.4.0",
42
+ "jest-extended": "4.0.2",
43
+ "vitest": "2.1.2"
44
+ },
45
+ "gitHead": "bcdaf87c198a588e02b5539c222f611e356d3079"
46
+ }