aberlaas-test 2.22.3 → 2.24.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.
@@ -0,0 +1,5 @@
1
+ // We make bench publicly available inside of tests, so users won't need to
2
+ // import it
3
+ import { bench } from 'vitest';
4
+
5
+ globalThis.bench = bench;
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Make describeName available as a variable in the describe() body.
3
+ *
4
+ * Note: We need to replace the initial describe with a wrapped version that
5
+ * sets describeName when the callback is being executed. We need to be careful
6
+ * to correctly keep all the inner methods as well (like .only, .concurrent,
7
+ * etc).
8
+ **/
9
+ /* global beforeEach, expect */
10
+
11
+ // Keep reference to original method
12
+ const originalDescribe = globalThis.describe;
13
+
14
+ // New wrapping method, that sets describeName when the callback is executed
15
+ const wrappedDescribe = function (name, callback) {
16
+ originalDescribe(name, () => {
17
+ globalThis.describeName = name;
18
+ callback();
19
+ });
20
+ };
21
+
22
+ // Re-assign the method, but keep internal methods
23
+ globalThis.describe = Object.defineProperties(
24
+ wrappedDescribe,
25
+ Object.getOwnPropertyDescriptors(originalDescribe),
26
+ );
27
+
28
+ // Make describeName available in all it() callbacks
29
+ beforeEach(() => {
30
+ globalThis.describeName = expect
31
+ .getState()
32
+ .currentTestName.split(' > ')
33
+ .at(-2);
34
+ });
@@ -0,0 +1,3 @@
1
+ // fit and fdescribe to run only those tests
2
+ globalThis.fit = globalThis.it.only;
3
+ globalThis.fdescribe = globalThis.describe.only;
@@ -0,0 +1,43 @@
1
+ // Helpful matchers not included in Vitest
2
+ import { _ } from 'golgoth';
3
+ import { expect } from 'vitest';
4
+
5
+ expect.extend({
6
+ toInclude(received, expected) {
7
+ return {
8
+ pass: _.includes(received, expected),
9
+ message: () =>
10
+ this.isNot
11
+ ? `expected not to include ${expected}`
12
+ : `expected to include ${expected}`,
13
+ };
14
+ },
15
+
16
+ toBeString(received) {
17
+ return {
18
+ pass: _.isString(received),
19
+ message: () =>
20
+ this.isNot ? 'expected not to be a string' : 'expected to be a string',
21
+ };
22
+ },
23
+
24
+ toStartWith(received, expected) {
25
+ return {
26
+ pass: _.startsWith(received, expected),
27
+ message: () =>
28
+ this.isNot
29
+ ? `expected not to start with ${expected}`
30
+ : `expected to start with ${expected}`,
31
+ };
32
+ },
33
+
34
+ toEndWith(received, expected) {
35
+ return {
36
+ pass: _.endsWith(received, expected),
37
+ message: () =>
38
+ this.isNot
39
+ ? `expected not to end with ${expected}`
40
+ : `expected to end with ${expected}`,
41
+ };
42
+ },
43
+ });
@@ -0,0 +1,37 @@
1
+ import { PassThrough } from 'node:stream';
2
+
3
+ /**
4
+ * Allow mocking stdin in tests.
5
+ * Usage:
6
+ * mockStdin((stdin) => {
7
+ * // do something
8
+ * stdin.push('abc');
9
+ * });
10
+ * Initial code inspired by: https://github.com/sindresorhus/ora/blob/main/test.js
11
+ * @param {Function} callback Method called with a fake stdin as first argument
12
+ * @returns {any} Return value of the callback
13
+ **/
14
+ globalThis.mockStdin = function (callback) {
15
+ const originalStdinDescriptor = Object.getOwnPropertyDescriptor(
16
+ process,
17
+ 'stdin',
18
+ );
19
+
20
+ const fakeStdin = new PassThrough();
21
+ fakeStdin.isTTY = true;
22
+ fakeStdin.isRaw = false;
23
+ fakeStdin.setRawMode = (value) => {
24
+ fakeStdin.isRaw = value;
25
+ };
26
+
27
+ Object.defineProperty(process, 'stdin', {
28
+ value: fakeStdin,
29
+ configurable: true,
30
+ });
31
+
32
+ try {
33
+ return callback(fakeStdin);
34
+ } finally {
35
+ Object.defineProperty(process, 'stdin', originalStdinDescriptor);
36
+ }
37
+ };
@@ -0,0 +1,3 @@
1
+ // xit and xdescribe to skip running those tests
2
+ globalThis.xit = globalThis.it.skip;
3
+ globalThis.xdescribe = globalThis.describe.skip;
@@ -0,0 +1,16 @@
1
+ // Add .slow() method to it/test/describe for tests that need more time
2
+ import { vi } from 'vitest';
3
+ import { slowPrefix, slowTimeout } from '../../configs/slow.js';
4
+
5
+ // Wrapper for it.slow()
6
+ globalThis.it.slow = (name, callback, timeout = slowTimeout) => {
7
+ return globalThis.it(`${slowPrefix}${name}`, callback, timeout);
8
+ };
9
+
10
+ // Wrapper for describe.slow()
11
+ globalThis.describe.slow = (name, callback) => {
12
+ return globalThis.describe(`${slowPrefix}${name}`, () => {
13
+ vi.setConfig({ testTimeout: slowTimeout });
14
+ callback();
15
+ });
16
+ };
@@ -1,9 +1,6 @@
1
1
  /* global beforeEach, expect */
2
- /**
3
- * Make the variable `testName` contain the name of the current test in each
4
- * test
5
- */
2
+
3
+ // Make testName available in all it() callbacks
6
4
  beforeEach(() => {
7
- const fullName = expect.getState().currentTestName;
8
- globalThis.testName = fullName.split(' > ').pop();
5
+ globalThis.testName = expect.getState().currentTestName.split(' > ').at(-1);
9
6
  });
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Configuration for slow tests
3
+ */
4
+
5
+ // Timeout of slow tests
6
+ export const slowTimeout = 30_000;
7
+
8
+ // Prefix of slow tests
9
+ export const slowPrefix = 'slow/';
package/configs/vite.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { dirname } from 'firost';
2
2
  import { defaultExclude, defineConfig } from 'vitest/config';
3
3
 
4
- const aberlaasVitestExclude = [...defaultExclude, '**/tmp/**'];
4
+ const aberlaasVitestExclude = [...defaultExclude, '**/tmp/**', '**/*.bench.js'];
5
5
 
6
6
  const configDir = dirname();
7
7
 
@@ -14,21 +14,32 @@ export default defineConfig({
14
14
  // Hide skipped tests, allowing less noisy debug with fit/fdescribe
15
15
  hideSkippedTests: true,
16
16
 
17
+ // Display details of tests as they run
18
+ reporters: ['tree'],
19
+
17
20
  // Tests should be in a __tests__ folder next to their code
18
21
  include: ['**/__tests__/**/*.js?(x)'],
19
22
  // We ignore temporary folders from the tests
20
23
  exclude: aberlaasVitestExclude,
21
- // Restore mocks after each tests
24
+
25
+ // Restore mocks to their real implementation between tests
22
26
  restoreMocks: true,
27
+ // Clear the number of time a mock has been called between tests
28
+ clearMocks: true,
23
29
 
24
30
  // Make describe, it, beforeEach and other globally available
25
31
  globals: true,
26
32
  // Run before each test file
27
33
  setupFiles: [
28
- `${configDir}/setupFiles/dedent.js`,
34
+ `${configDir}/setupFiles/bench.js`,
29
35
  `${configDir}/setupFiles/captureOutput.js`,
30
- `${configDir}/setupFiles/fit-xit-fdescribe-xdescribe.js`,
31
- `${configDir}/setupFiles/jest-extended.js`,
36
+ `${configDir}/setupFiles/dedent.js`,
37
+ `${configDir}/setupFiles/describeName.js`,
38
+ `${configDir}/setupFiles/focus.js`,
39
+ `${configDir}/setupFiles/matchers.js`,
40
+ `${configDir}/setupFiles/mockStdin.js`,
41
+ `${configDir}/setupFiles/skip.js`,
42
+ `${configDir}/setupFiles/slow.js`,
32
43
  `${configDir}/setupFiles/testName.js`,
33
44
  ],
34
45
  },
package/lib/main.js CHANGED
@@ -2,6 +2,7 @@ import { _ } from 'golgoth';
2
2
  import { firostError } from 'firost';
3
3
  import { getConfig, hostPackageRoot } from 'aberlaas-helper';
4
4
  import { createVitest, registerConsoleShortcuts } from 'vitest/node';
5
+ import { slowPrefix } from '../configs/slow.js';
5
6
  import viteConfig from '../configs/vite.js';
6
7
 
7
8
  export let __;
@@ -13,7 +14,8 @@ export let __;
13
14
  * $ aberlaas test ./path/to/__tests__/file.js # Test specific files
14
15
  * $ aberlaas test ./path/to/file.js # Test specific files
15
16
  * $ aberlaas test --related # Test all related files
16
- * $ aberlaas test --failFast # Stop early as soon as one test fails
17
+ * $ aberlaas test --fail-fast # Stop early as soon as one test fails
18
+ * $ aberlaas test --only-slow # Run only tests marked with .slow()
17
19
  * $ aberlaas test --flags # Flags passed to vitest
18
20
  * @param {object} cliArgs CLI Argument object, as created by minimist
19
21
  * @returns {boolean} true on success
@@ -80,9 +82,10 @@ __ = {
80
82
  const specialMeaningCliArgs = [
81
83
  '_',
82
84
  'config',
83
- 'failFast',
85
+ 'fail-fast',
84
86
  'related',
85
87
  'exclude',
88
+ 'only-slow',
86
89
  ];
87
90
 
88
91
  // Reading base options from the config file
@@ -99,10 +102,14 @@ __ = {
99
102
  // by the lint command instead
100
103
  allowOnly: true,
101
104
  };
102
- // --failFast stops early as soon as one test fails
103
- if (cliArgs.failFast) {
105
+ // --fail-fast stops early as soon as one test fails
106
+ if (cliArgs['fail-fast']) {
104
107
  optionsFromAberlaas.bail = 1;
105
108
  }
109
+ // --only-slow runs only tests marked with .slow()
110
+ if (cliArgs['only-slow']) {
111
+ optionsFromAberlaas.testNamePattern = _.escapeRegExp(slowPrefix);
112
+ }
106
113
  // --related runs also related files
107
114
  // Note (2024-10-01): The related option is not documented, but should
108
115
  // contain the list of files.
@@ -111,7 +118,6 @@ __ = {
111
118
  }
112
119
  // --exclude arguments should be added to the existing list of exclude
113
120
  // patterns
114
- // TODO: Add test for that
115
121
  if (cliArgs.exclude) {
116
122
  optionsFromAberlaas.exclude = [
117
123
  ...optionsFromConfig.exclude,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aberlaas-test",
3
- "version": "2.22.3",
3
+ "version": "2.24.0",
4
4
  "type": "module",
5
5
  "description": "aberlaas test command: Run automated tests",
6
6
  "author": "Tim Carry <tim@pixelastic.com>",
@@ -21,13 +21,12 @@
21
21
  },
22
22
  "main": "./lib/main.js",
23
23
  "dependencies": {
24
- "aberlaas-helper": "2.22.3",
24
+ "aberlaas-helper": "2.24.0",
25
25
  "dedent": "1.7.1",
26
- "firost": "5.5.1",
26
+ "firost": "5.6.1",
27
27
  "globals": "17.2.0",
28
28
  "golgoth": "3.1.0",
29
- "jest-extended": "4.0.2",
30
- "vitest": "2.1.2"
29
+ "vitest": "4.0.18"
31
30
  },
32
31
  "scripts": {
33
32
  "build": "cd ../docs && yarn run build",
@@ -1,13 +0,0 @@
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;
@@ -1,10 +0,0 @@
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 * as matchers from 'jest-extended';
8
- import { expect } from 'vitest';
9
-
10
- expect.extend(matchers);