sxy-test-runner 1.0.11 → 1.0.13

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.
@@ -17,7 +17,7 @@ export async function showTestLoadingError(config, test, customOut = undefined)
17
17
  const indent = ' ';
18
18
  out();
19
19
  out(chalk.grey`File ${test.file}`);
20
- out(chalk.mediumred`Loading error: ${test.error}`);
20
+ out(chalk.mediumred`Loading error:\n${test.error}`);
21
21
 
22
22
  if (test.logs.length > 0) {
23
23
  out();
@@ -10,14 +10,17 @@ await mochaDescribe('showTestResult() function', async function () {
10
10
  const sampleTestResult = {
11
11
  file: 'unitTesting/sampleTests/1.test.js',
12
12
  success: true,
13
+ logs: [],
13
14
  describeResults: [{
14
15
  counter: 1,
15
16
  thing: 'Sample test 1',
16
17
  success: true,
18
+ logs: [],
17
19
  itResults: [{
18
20
  counter: 1,
19
21
  should: '1 should equal 1',
20
22
  success: true,
23
+ logs: [],
21
24
  time: 1,
22
25
  error: undefined
23
26
  }],
@@ -62,14 +65,17 @@ await mochaDescribe('showTestResult() function', async function () {
62
65
  const sampleTestResult = {
63
66
  file: 'unitTesting/sampleTests/1.test.js',
64
67
  success: true,
68
+ logs: [],
65
69
  describeResults: [{
66
70
  counter: 1,
67
71
  thing: 'Sample test 1',
68
72
  success: true,
73
+ logs: [],
69
74
  itResults: [{
70
75
  counter: 1,
71
76
  should: '1 should equal 2',
72
77
  success: false,
78
+ logs: [],
73
79
  error
74
80
  }],
75
81
  itResultsSummary: {
@@ -103,6 +109,7 @@ await mochaDescribe('showTestResult() function', async function () {
103
109
  const sampleTestResult = {
104
110
  file: 'unitTesting/sampleTests/1.test.js',
105
111
  success: true,
112
+ logs: [],
106
113
  describeResults: [{
107
114
  counter: 1,
108
115
  thing: 'Sample test 1',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sxy-test-runner",
3
- "version": "1.0.11",
3
+ "version": "1.0.13",
4
4
  "license": "UNLICENSED",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -40,30 +40,33 @@
40
40
  "mocha-trial": "mocha --watch --parallel --config unitTesting/.mocharc.cjs --watch-files dist,unitTesting dist/**/runTest.unitTest.js"
41
41
  },
42
42
  "dependencies": {
43
- "@babel/core": "^7.18.5",
43
+ "@babel/core": "^7.20.2",
44
44
  "chalk-extensions": "^1.0.1",
45
+ "chokidar": "^3.5.3",
45
46
  "cross-env": "^7.0.3",
46
47
  "dependency-tree": "^8.1.2",
47
48
  "figures": "^4.0.1",
48
49
  "glob": "^7.2.3",
49
50
  "globby": "^12.2.0",
51
+ "minimatch": "^5.1.0",
50
52
  "node-watch": "^0.7.3",
53
+ "slash": "^5.0.0",
51
54
  "sxy-dev-tools": "^1.0.6",
52
55
  "sxy-helpers": "^1.0.0",
53
- "sxy-loader": "^2.0.9",
56
+ "sxy-loader": "^2.0.18",
54
57
  "sxy-standard": "^1.0.11",
55
58
  "sxy-standard-object-copy": "^1.0.5"
56
59
  },
57
60
  "devDependencies": {
58
- "@babel/cli": "^7.17.10",
61
+ "@babel/cli": "^7.19.3",
59
62
  "babel-plugin-transform-awaitful": "^1.0.0",
60
- "chai": "^4.3.6",
63
+ "chai": "^4.3.7",
61
64
  "chai-as-promised": "^7.1.1",
62
65
  "eslint": "^7.32.0",
63
66
  "eslint-config-sandi": "^1.2.0",
64
67
  "mocha": "^9.2.2",
65
68
  "mocha-steps": "^1.3.0",
66
- "nodemon": "^2.0.16",
69
+ "nodemon": "^2.0.20",
67
70
  "sinon": "^12.0.1",
68
71
  "sinon-chai": "^3.7.0"
69
72
  }
package/readme.md CHANGED
@@ -1,206 +1,206 @@
1
- # sxy-test-runner
2
-
3
- Mocha-like test runner for **es modules** (only), with **watch mode**, and **re-running only tests related to changes** (by dependency analysis)
4
-
5
- ```js
6
- import { describe } from 'sxy-test-runner'
7
- import { should as setupShould } from 'chai'
8
- const should = setupShould()
9
-
10
- function add(a, b) {
11
- return a + b
12
- }
13
-
14
- describe('The add() function', ({it}) => {
15
- it('should add 1 and 2, making 3', () => {
16
- const result = add(1, 2)
17
- result.should.equal(3)
18
- }
19
- })
20
- ```
21
-
22
- ## Usage
23
-
24
- ### 1. Install
25
-
26
- ```
27
- npm i -D sxy-test-runner
28
- ```
29
-
30
- *or*
31
-
32
- ```
33
- yarn add -D sxy-test-runner
34
- ```
35
-
36
- *or*
37
-
38
- ```
39
- pnpm add -D sxy-test-runner
40
- ```
41
-
42
- ---
43
-
44
- ### 2. Create a config (required)
45
-
46
- ```
47
- sxy-test-runner init
48
- ```
49
- *sxy-test is an alias for sxy-test-runner and can be used instead*
50
-
51
- This will create a config in your project folder. It can be moved to a folder named "config" if you prefer, or specify a location using --config
52
-
53
- ### 3. Modify the config, specifying the base path for tests and code to watch, and glob patterns for tests and code to watch
54
-
55
- ```js
56
- {
57
- // base folder for tests matching and ignore patterns
58
- testsBase: 'dist',
59
-
60
- // glob pattern or array of glob patterns of test files
61
- tests: [
62
- '**/*.test.{js,jsx}'
63
- ],
64
-
65
- // watch mode configurations
66
- watch: {
67
-
68
- //// the base directory to watch files in
69
- watchFilesBase: 'dist',
70
-
71
- //// glob filter or array of global filters of files to watch
72
- watchFiles: '**/*.js',
73
-
74
- },
75
- }
76
- ```
77
-
78
- The other config options and fairly well explained in the config. More info on them later.
79
-
80
- ### 4. Run
81
-
82
- ```
83
- npx sxy-test-runner
84
- ```
85
- *sxy-test is an alias for sxy-test-runner and can be used instead*
86
-
87
- ("npx" can be omitted in package.json scripts)
88
-
89
- sxy-test-runner runs watch mode, re-running only relevant tests, by default.
90
-
91
- To run tests only once and not watch (e.g. for ci/cd), use
92
- ```
93
- npx sxy-test-runner once
94
- ```
95
-
96
- To specific an alternate config, use -c or --config
97
- ```
98
- npx sxy-test-runner --config sxy-test-runner.alternate.config.js
99
- ```
100
-
101
- # How it works
102
-
103
- sxy-test-runner runs all tests inside one node process, to improve performance (this framework far exceeds mocha in parallel mode, which is needed to test ESM files, due to this).
104
-
105
- Because of this, be careful with globals, which will be shared.
106
-
107
- ## Execution modes
108
-
109
- **execution** settings let you choose whether to run test files, describe blocks, and individual tests sequentially or in parallel.
110
-
111
- If working with globals you may want to use sequential only, to avoid conflicts on those globals.
112
-
113
- Sequential mode also allows console log output to be displayed correctly with each test. Running in parallel mode does not allow log output to be matched up to each test, due to the use of a global override on console.log (other console output methods such as console.trace() are not covered currently)
114
-
115
- If your code is isolated, and asyncronous, you can use parallel exeecution modes to radically speed up your tests, as the expense of the points mentioned above.
116
-
117
- ## Dependencies
118
-
119
- Module dependencies are identified using dependency-tree. Dynamic imports will not be detected and will not trigger test re-runs.
120
-
121
- No option to re-run all tests on changes is currently available - I may add this on request, or submit a PR if you want this.
122
-
123
- ## Failing test re-running
124
-
125
- Default behaviour is to re-run any failing test any time a change is made, even if that change isn't relevant to the failing test. This is to ensure you can see where attention is needed. Turn this off in the config if it is not convenient for you: config.watch.reRunFailingTests
126
-
127
- ## Reloading modules
128
-
129
- A key challenge in creating an ESM test runner is re-loading modules due to the engine not letting us clear the import cache of ESM modules. This framework relies on sxy-loader, which uses a babel transform to transform modules into a reloadable format with a controllable cache. A cache of transformed code is kept to keep the process speedy.
130
-
131
- Performance is a little less than a native module import, but not so much as to be notable inconvenience. Re-running only test related to changes keep performance strong even in very large projects.
132
-
133
- # Advanced usage
134
-
135
- ## 1. Run tasks at startup - setup
136
-
137
- Edit config.setup. This config value can take a function, a file to import relative to the project folder, or an array or one or both of those.
138
-
139
- ## 2. Run tasks before each test file / describe / test globally
140
-
141
- Edit config.beforeEachFile / config.beforeEachDescribe / config.beforeEachTest with a function, file or array of either.
142
-
143
- If a file exports named values, or a function returns an object with properties, these keys will be added to a storage object (beware of conflicting names). This object is then passed to the afterEachFile / afterEachDescribe / afterEachTest tasks for cleanup.
144
-
145
- ```js
146
- // tasks to run before each test file is run
147
- beforeEachFile: () => {
148
- const thing = prepSomething()
149
- return { thing }
150
- },
151
-
152
- // tasks to run after each test file is run
153
- afterEachFile: ({thing}) => {
154
- destroyingSomething(thing)
155
- },
156
- ```
157
-
158
- ## 3. Run code before each describe block, or at the end of a test file - per test file
159
-
160
- Not yet implemented.
161
-
162
- ## 4. Run code before each test, after each test, or after the describe block - per describe block
163
-
164
- ```js
165
- import { describe } from 'sxy-test-runner'
166
- import { should as setupShould } from 'chai'
167
- const should = setupShould()
168
-
169
- describe('something', ({it, beforeEachTest, afterEachTest, afterDescribe}) => {
170
-
171
- // setup can take place here without any special measures
172
- // but you can only safely set globals for your test here if your
173
- // execution mode for files and describes is 'sequential' (default)
174
- const something = createSomething()
175
-
176
- // run this just before each test
177
- beforeEachTest(() => {
178
- prepSomething(something)
179
- })
180
-
181
- // run this just after each test
182
- afterEachTest(() => {
183
- clearupSomething(something)
184
- })
185
-
186
- it('should do something', () => {
187
- something.should.do.something
188
- })
189
- it('should do another thing', () => {
190
- something.should.do.another.thing
191
- })
192
-
193
- // teardown should go in afterDescribe.
194
- // tests are not run syncronously within the describe block (even sync tests)
195
- // so cleanup code here would run too early
196
- afterDescribe(() => {
197
- destroySomething(something)
198
- })
199
-
200
- })
201
-
202
- ```
203
-
204
- # Known issues
205
-
1
+ # sxy-test-runner
2
+
3
+ Mocha-like test runner for **es modules** (only), with **watch mode**, and **re-running only tests related to changes** (by dependency analysis), full **async code support**
4
+
5
+ ```js
6
+ import { describe } from 'sxy-test-runner'
7
+ import { should as setupShould } from 'chai'
8
+ const should = setupShould()
9
+
10
+ function add(a, b) {
11
+ return a + b
12
+ }
13
+
14
+ describe('The add() function', ({it}) => {
15
+ it('should add 1 and 2, making 3', () => {
16
+ const result = add(1, 2)
17
+ result.should.equal(3)
18
+ }
19
+ })
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ ### 1. Install
25
+
26
+ ```
27
+ npm i -D sxy-test-runner
28
+ ```
29
+
30
+ *or*
31
+
32
+ ```
33
+ yarn add -D sxy-test-runner
34
+ ```
35
+
36
+ *or*
37
+
38
+ ```
39
+ pnpm add -D sxy-test-runner
40
+ ```
41
+
42
+ ---
43
+
44
+ ### 2. Create a config (required)
45
+
46
+ ```
47
+ sxy-test-runner init
48
+ ```
49
+ *sxy-test is an alias for sxy-test-runner and can be used instead*
50
+
51
+ This will create a config in your project folder. It can be moved to a folder named "config" if you prefer, or specify a location using --config
52
+
53
+ ### 3. Modify the config, specifying the base path for tests and code to watch, and glob patterns for tests and code to watch
54
+
55
+ ```js
56
+ {
57
+ // base folder for tests matching and ignore patterns
58
+ testsBase: 'dist',
59
+
60
+ // glob pattern or array of glob patterns of test files
61
+ tests: [
62
+ '**/*.test.{js,jsx}'
63
+ ],
64
+
65
+ // watch mode configurations
66
+ watch: {
67
+
68
+ //// the base directory to watch files in
69
+ watchFilesBase: 'dist',
70
+
71
+ //// glob filter or array of global filters of files to watch
72
+ watchFiles: '**/*.js',
73
+
74
+ },
75
+ }
76
+ ```
77
+
78
+ The other config options and fairly well explained in the config. More info on them later.
79
+
80
+ ### 4. Run
81
+
82
+ ```
83
+ npx sxy-test-runner
84
+ ```
85
+ *sxy-test is an alias for sxy-test-runner and can be used instead*
86
+
87
+ ("npx" can be omitted in package.json scripts)
88
+
89
+ sxy-test-runner runs watch mode, re-running only relevant tests, by default.
90
+
91
+ To run tests only once and not watch (e.g. for ci/cd), use
92
+ ```
93
+ npx sxy-test-runner once
94
+ ```
95
+
96
+ To specific an alternate config, use -c or --config
97
+ ```
98
+ npx sxy-test-runner --config sxy-test-runner.alternate.config.js
99
+ ```
100
+
101
+ # How it works
102
+
103
+ sxy-test-runner runs all tests inside one node process, to improve performance (this framework far exceeds mocha in parallel mode, which is needed to test ESM files, due to this).
104
+
105
+ Because of this, be careful with globals, which will be shared.
106
+
107
+ ## Execution modes
108
+
109
+ **execution** settings let you choose whether to run test files, describe blocks, and individual tests sequentially or in parallel.
110
+
111
+ If working with globals you may want to use sequential only, to avoid conflicts on those globals.
112
+
113
+ Sequential mode also allows console log output to be displayed correctly with each test. Running in parallel mode does not allow log output to be matched up to each test, due to the use of a global override on console.log (other console output methods such as console.trace() are not covered currently)
114
+
115
+ If your code is isolated, and asyncronous, you can use parallel exeecution modes to radically speed up your tests, as the expense of the points mentioned above.
116
+
117
+ ## Dependencies
118
+
119
+ Module dependencies are identified using dependency-tree. Dynamic imports will not be detected and will not trigger test re-runs.
120
+
121
+ No option to re-run all tests on changes is currently available - I may add this on request, or submit a PR if you want this.
122
+
123
+ ## Failing test re-running
124
+
125
+ Default behaviour is to re-run any failing test any time a change is made, even if that change isn't relevant to the failing test. This is to ensure you can see where attention is needed. Turn this off in the config if it is not convenient for you: config.watch.reRunFailingTests
126
+
127
+ ## Reloading modules
128
+
129
+ A key challenge in creating an ESM test runner is re-loading modules due to the engine not letting us clear the import cache of ESM modules. This framework relies on sxy-loader, which uses a babel transform to transform modules into a reloadable format with a controllable cache. A cache of transformed code is kept to keep the process speedy.
130
+
131
+ Performance is a little less than a native module import, but not so much as to be notable inconvenience. Re-running only test related to changes keep performance strong even in very large projects.
132
+
133
+ # Advanced usage
134
+
135
+ ## 1. Run tasks at startup - setup
136
+
137
+ Edit config.setup. This config value can take a function, a file to import relative to the project folder, or an array or one or both of those.
138
+
139
+ ## 2. Run tasks before each test file / describe / test globally
140
+
141
+ Edit config.beforeEachFile / config.beforeEachDescribe / config.beforeEachTest with a function, file or array of either.
142
+
143
+ If a file exports named values, or a function returns an object with properties, these keys will be added to a storage object (beware of conflicting names). This object is then passed to the afterEachFile / afterEachDescribe / afterEachTest tasks for cleanup.
144
+
145
+ ```js
146
+ // tasks to run before each test file is run
147
+ beforeEachFile: () => {
148
+ const thing = prepSomething()
149
+ return { thing }
150
+ },
151
+
152
+ // tasks to run after each test file is run
153
+ afterEachFile: ({thing}) => {
154
+ destroyingSomething(thing)
155
+ },
156
+ ```
157
+
158
+ ## 3. Run code before each describe block, or at the end of a test file - per test file
159
+
160
+ Not yet implemented.
161
+
162
+ ## 4. Run code before each test, after each test, or after the describe block - per describe block
163
+
164
+ ```js
165
+ import { describe } from 'sxy-test-runner'
166
+ import { should as setupShould } from 'chai'
167
+ const should = setupShould()
168
+
169
+ describe('something', ({it, beforeEachTest, afterEachTest, afterDescribe}) => {
170
+
171
+ // setup can take place here without any special measures
172
+ // but you can only safely set globals for your test here if your
173
+ // execution mode for files and describes is 'sequential' (default)
174
+ const something = createSomething()
175
+
176
+ // run this just before each test
177
+ beforeEachTest(() => {
178
+ prepSomething(something)
179
+ })
180
+
181
+ // run this just after each test
182
+ afterEachTest(() => {
183
+ clearupSomething(something)
184
+ })
185
+
186
+ it('should do something', () => {
187
+ something.should.do.something
188
+ })
189
+ it('should do another thing', () => {
190
+ something.should.do.another.thing
191
+ })
192
+
193
+ // teardown should go in afterDescribe.
194
+ // tests are not run syncronously within the describe block (even sync tests)
195
+ // so cleanup code here would run too early
196
+ afterDescribe(() => {
197
+ destroySomething(something)
198
+ })
199
+
200
+ })
201
+
202
+ ```
203
+
204
+ # Known issues
205
+
206
206
  sxy-loader uses eval to load and re-load modules, and this makes a big mess of error output. I am hoping for a solution to module reloading in the official spec to solve this. It may be possible to switch to a different method of re-loading modules to solve this (no plans for this just now).
@@ -4,7 +4,7 @@ export default {
4
4
  mode: process.env.NODE_ENV, // 'development' | 'production' environment
5
5
  //transformCaching: 'content', // content | hash | false // default content
6
6
  //transformCacheStorage: 'file', // 'file' | 'memory'
7
- transformCacheStorageLocation: 'loaderCache', // null | path/to/location/from/project/dir
7
+ //transformCacheStorageLocation: '__cache', // null | path/to/location/from/project/dir
8
8
  quiet: true, // disable info output
9
9
  debug: false, // debugging output // doesn't work yet
10
10
  }
@@ -1,35 +0,0 @@
1
- import { runTasks } from './runTasks.js';
2
- import { addExports } from './addExports.js';
3
- export async function configBeforeEachFile(config, exports) {
4
- if (config.beforeEachFile) {
5
- const theExports = await runTasks(config.beforeEachFile, undefined);
6
- await addExports(exports, theExports);
7
- }
8
- }
9
- export async function configAfterEachFile(config, exports) {
10
- if (config.afterEachFile) {
11
- await runTasks(config.afterEachFile, exports);
12
- }
13
- }
14
- export async function configBeforeEachDescribe(config, exports) {
15
- if (config.beforeEachDescribe) {
16
- const theExports = await runTasks(config.beforeEachDescribe, undefined);
17
- await addExports(exports, theExports);
18
- }
19
- }
20
- export async function configAfterEachDescribe(config, exports) {
21
- if (config.afterEachDescribe) {
22
- await runTasks(config.afterEachDescribe, exports);
23
- }
24
- }
25
- export async function configBeforeEachTest(config, exports) {
26
- if (config.beforeEachTest) {
27
- const theExports = await runTasks(config.beforeEachTest, undefined);
28
- await addExports(exports, theExports);
29
- }
30
- }
31
- export async function configAfterEachTest(config, exports) {
32
- if (config.afterEachTest) {
33
- await runTasks(config.afterEachTest, exports);
34
- }
35
- }
File without changes
@@ -1,20 +0,0 @@
1
- export async function overrideConsoleLog() {
2
- // save the original func
3
- const originalConsoleLog = console.log; // eslint-disable-line no-console
4
- // create a catcher func
5
-
6
- async function logCatcher(...texts) {
7
- await logCatcher.logs.push(texts);
8
- } // initise a variable on it
9
-
10
-
11
- logCatcher.logs = []; // assign the new func to console.log
12
-
13
- console.log = logCatcher; // eslint-disable-line no-console
14
- // return the original func (for restoring) and the new func (for retreiving caught logging)
15
-
16
- return {
17
- originalConsoleLog,
18
- replacementConsoleLog: logCatcher
19
- };
20
- }
File without changes