typenative 0.4.0 → 0.5.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/.github/workflows/ci.yml +2 -0
- package/CHANGELOG.md +372 -0
- package/CONTRIBUTION.md +1 -1
- package/README.md +62 -22
- package/ROUND2-HANDOFF.md +200 -0
- package/bin/cli.js +2 -0
- package/bin/go/bridge.js +74 -14
- package/bin/go/convert.js +48 -0
- package/bin/go/metadata.js +13 -4
- package/bin/go/overrides.js +4 -6
- package/bin/go/stdlib.generated.js +2 -0
- package/bin/index.js +130 -2
- package/bin/main.js +2 -0
- package/bin/node/assert.js +2 -2
- package/bin/node/async_hooks.js +1 -1
- package/bin/node/buffer.js +16 -5
- package/bin/node/child_process.js +57 -9
- package/bin/node/cluster.js +44 -6
- package/bin/node/core.js +1 -1
- package/bin/node/crypto.js +205 -4
- package/bin/node/crypto_keys.js +21 -21
- package/bin/node/dgram.js +231 -7
- package/bin/node/diagnostics_channel.js +3 -3
- package/bin/node/dns.js +472 -9
- package/bin/node/errno_tables.js +445 -0
- package/bin/node/events.js +3 -3
- package/bin/node/fetch.js +2 -2
- package/bin/node/fs.js +24 -17
- package/bin/node/fs_watch.js +5 -5
- package/bin/node/http.js +131 -33
- package/bin/node/http2.js +7 -4
- package/bin/node/module_v8.js +142 -0
- package/bin/node/net.js +17 -14
- package/bin/node/path.js +17 -3
- package/bin/node/process.js +274 -9
- package/bin/node/readline.js +272 -1
- package/bin/node/registry.js +5 -1
- package/bin/node/sqlite.js +65 -17
- package/bin/node/stream.js +220 -22
- package/bin/node/stream_web.js +10 -10
- package/bin/node/system_extra.js +3 -3
- package/bin/node/test.js +834 -234
- package/bin/node/test_coverage.js +187 -0
- package/bin/node/test_report.js +1392 -0
- package/bin/node/test_run.js +575 -0
- package/bin/node/timers.js +4 -4
- package/bin/node/tls.js +5 -5
- package/bin/node/url.js +23 -5
- package/bin/node/util.js +49 -8
- package/bin/node/util_extra.js +13 -37
- package/bin/node/webview.js +654 -0
- package/bin/node/worker_threads.js +80 -20
- package/bin/node/zlib.js +3 -3
- package/bin/parse-node.js +4 -0
- package/bin/transpiler.js +14457 -1723
- package/bin/typecheck-node.js +1 -0
- package/bin/version.js +1 -1
- package/conformance.md +1153 -884
- package/examples/README.md +47 -10
- package/examples/desktop-notes/README.md +108 -0
- package/examples/desktop-notes/bench.mjs +125 -0
- package/examples/desktop-notes/main.ts +131 -0
- package/examples/desktop-notes/package-lock.json +726 -0
- package/examples/desktop-notes/package.json +9 -0
- package/examples/desktop-notes/ui.ts +176 -0
- package/examples/dupe-finder/README.md +76 -0
- package/examples/dupe-finder/bench.mjs +120 -0
- package/examples/dupe-finder/main.ts +87 -0
- package/examples/json-server/README.md +31 -29
- package/examples/json-server/bench.mjs +130 -38
- package/examples/mandelbrot-png/README.md +65 -0
- package/examples/mandelbrot-png/bench.mjs +95 -0
- package/examples/mandelbrot-png/main.ts +98 -0
- package/examples/mandelbrot-png/preview.png +0 -0
- package/examples/marked-cli/README.md +159 -0
- package/examples/marked-cli/bench.mjs +178 -0
- package/examples/marked-cli/main.ts +292 -0
- package/examples/marked-cli/parity.mjs +185 -0
- package/examples/marked-cli/vendor/marked/LICENSE +44 -0
- package/examples/marked-cli/vendor/marked/VENDOR.md +26 -0
- package/examples/todo-cli/README.md +69 -0
- package/examples/todo-cli/bench.mjs +89 -0
- package/examples/todo-cli/main.ts +92 -0
- package/examples/yaml-cli/README.md +136 -0
- package/examples/yaml-cli/bench.mjs +265 -0
- package/examples/yaml-cli/main.ts +29 -0
- package/examples/yaml-cli/parity.mjs +156 -0
- package/examples/yaml-cli/vendor/yaml/LICENSE +13 -0
- package/examples/yaml-cli/vendor/yaml/VENDOR.md +25 -0
- package/govendor/github.com/jchv/go-webview2/LICENSE +22 -0
- package/govendor/github.com/jchv/go-webview2/README.md +21 -0
- package/govendor/github.com/jchv/go-webview2/common.go +84 -0
- package/govendor/github.com/jchv/go-webview2/go.mod +3 -0
- package/govendor/github.com/jchv/go-webview2/internal/w32/w32.go +194 -0
- package/govendor/github.com/jchv/go-webview2/internal/w32/w32_386.go +14 -0
- package/govendor/github.com/jchv/go-webview2/internal/w32/w32_64bit.go +15 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_COLOR.go +8 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND.go +9 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_KEY_EVENT_KIND.go +10 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_MOVE_FOCUS_REASON.go +9 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_PHYSICAL_KEY_STATUS.go +10 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_WEB_RESOURCE_CONTEXT.go +23 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2AcceleratorKeyPressedEventArgs.go +78 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2AcceleratorKeyPressedEventHandler.go +52 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2Controller.go +132 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2Controller2.go +74 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2CreateCoreWebView2ControllerCompletedHandler.go +48 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2NavigationCompletedEventArgs.go +17 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2NavigationCompletedEventHandler.go +48 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2Settings.go +270 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2WebResourceRequest.go +43 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2WebResourceRequestedEventArgs.go +51 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2WebResourceRequestedEventHandler.go +48 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2WebResourceResponse.go +21 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2_2.go +21 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2_3.go +60 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebViewSettings.go +396 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/chromium.go +361 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/chromium_386.go +24 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/chromium_amd64.go +22 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/chromium_arm64.go +26 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/comproc.go +13 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/comproc_go17.go +51 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/comproc_go18.go +16 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/corewebview2.go +411 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/guid.go +223 -0
- package/govendor/github.com/jchv/go-webview2/webview.go +482 -0
- package/govendor/github.com/jchv/go-webview2/webviewloader/README.md +3 -0
- package/govendor/github.com/jchv/go-webview2/webviewloader/module.go +153 -0
- package/govendor/github.com/jchv/go-webview2/webviewloader/module_386.go +6 -0
- package/govendor/github.com/jchv/go-webview2/webviewloader/module_amd64.go +6 -0
- package/govendor/github.com/jchv/go-webview2/webviewloader/module_arm64.go +6 -0
- package/govendor/github.com/jchv/go-webview2/webviewloader/sdk/arm64/WebView2Loader.dll +0 -0
- package/govendor/github.com/jchv/go-webview2/webviewloader/sdk/x64/WebView2Loader.dll +0 -0
- package/govendor/github.com/jchv/go-webview2/webviewloader/sdk/x86/WebView2Loader.dll +0 -0
- package/govendor/github.com/jchv/go-winloader/LICENSE.md +7 -0
- package/govendor/github.com/jchv/go-winloader/README.md +124 -0
- package/govendor/github.com/jchv/go-winloader/common.go +11 -0
- package/govendor/github.com/jchv/go-winloader/go.mod +3 -0
- package/govendor/github.com/jchv/go-winloader/internal/loader/loader.go +25 -0
- package/govendor/github.com/jchv/go-winloader/internal/loader/machine.go +53 -0
- package/govendor/github.com/jchv/go-winloader/internal/memloader/cache.go +42 -0
- package/govendor/github.com/jchv/go-winloader/internal/memloader/loader.go +253 -0
- package/govendor/github.com/jchv/go-winloader/internal/pe/export.go +76 -0
- package/govendor/github.com/jchv/go-winloader/internal/pe/format.go +522 -0
- package/govendor/github.com/jchv/go-winloader/internal/pe/import.go +106 -0
- package/govendor/github.com/jchv/go-winloader/internal/pe/module.go +91 -0
- package/govendor/github.com/jchv/go-winloader/internal/pe/reloc.go +177 -0
- package/govendor/github.com/jchv/go-winloader/internal/pe/util.go +40 -0
- package/govendor/github.com/jchv/go-winloader/internal/vmem/common.go +55 -0
- package/govendor/github.com/jchv/go-winloader/internal/vmem/memory_other.go +71 -0
- package/govendor/github.com/jchv/go-winloader/internal/vmem/memory_windows.go +179 -0
- package/govendor/github.com/jchv/go-winloader/internal/winloader/hacks_other.go +17 -0
- package/govendor/github.com/jchv/go-winloader/internal/winloader/hacks_windows.go +73 -0
- package/govendor/github.com/jchv/go-winloader/internal/winloader/loader_windows.go +64 -0
- package/govendor/github.com/jchv/go-winloader/internal/winloader/machine_windows.go +32 -0
- package/govendor/github.com/jchv/go-winloader/internal/winloader/nativearch_386.go +9 -0
- package/govendor/github.com/jchv/go-winloader/internal/winloader/nativearch_amd64.go +9 -0
- package/govendor/github.com/jchv/go-winloader/internal/winloader/nativearch_arm64.go +9 -0
- package/govendor/github.com/jchv/go-winloader/loader_other.go +24 -0
- package/govendor/github.com/jchv/go-winloader/loader_windows.go +33 -0
- package/govendor/manifest.txt +3 -1
- package/package.json +1 -1
- package/tsparser/main.go +4 -0
- package/types/typenative-go.d.ts +23 -0
- package/types/typenative-npm.d.ts +359 -7
- package/types/typenative.d.ts +283 -42
package/bin/node/test.js
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
// node:test — test / it / describe / suite with hooks, subtests (t.test),
|
|
2
|
-
// skip / todo, timeouts, async and callback-style tests, and
|
|
3
|
-
// are collected while the program runs and executed one after another
|
|
4
|
-
// the main module has finished (as in Node)
|
|
5
|
-
//
|
|
6
|
-
//
|
|
2
|
+
// skip / todo, timeouts, async and callback-style tests, mocks and snapshots.
|
|
3
|
+
// Tests are collected while the program runs and executed one after another
|
|
4
|
+
// once the main module has finished (as in Node). The runner reports through
|
|
5
|
+
// events (Node's test:start / test:pass / test:fail / …), which the
|
|
6
|
+
// reporters in test_report.ts turn into TAP, spec, dot, junit or lcov text
|
|
7
|
+
// for each --test-reporter / --test-reporter-destination pair. A failure
|
|
8
|
+
// sets the exit code to 1.
|
|
7
9
|
import { defineClass, defineHelper, defineModule, setResults } from './registry.js';
|
|
10
|
+
import { registerTestReporters } from './test_report.js';
|
|
11
|
+
import { registerTestRun } from './test_run.js';
|
|
12
|
+
import { registerTestCoverage } from './test_coverage.js';
|
|
8
13
|
const testHelper = `// node:test
|
|
9
14
|
type TnTestNode struct {
|
|
10
15
|
name string
|
|
@@ -14,6 +19,7 @@ type TnTestNode struct {
|
|
|
14
19
|
skipMsg string
|
|
15
20
|
todo bool
|
|
16
21
|
todoMsg string
|
|
22
|
+
only bool
|
|
17
23
|
timeoutMs float64
|
|
18
24
|
parent *TnTestNode
|
|
19
25
|
children []*TnTestNode
|
|
@@ -24,23 +30,30 @@ type TnTestNode struct {
|
|
|
24
30
|
status string
|
|
25
31
|
err interface{}
|
|
26
32
|
failureType string
|
|
33
|
+
failure *TnError
|
|
27
34
|
duration float64
|
|
28
35
|
diagnostics []string
|
|
29
36
|
started bool
|
|
30
|
-
headerShown bool
|
|
31
37
|
ctx *TnTestContext
|
|
32
38
|
done chan struct{}
|
|
39
|
+
// where test() / describe() was called
|
|
40
|
+
file string
|
|
41
|
+
line float64
|
|
42
|
+
column float64
|
|
33
43
|
}
|
|
34
44
|
|
|
35
45
|
var tnTestRoot = &TnTestNode{suite: true}
|
|
36
46
|
var tnTestCurrent = tnTestRoot
|
|
37
47
|
var tnTestRunning *TnTestNode
|
|
38
48
|
var tnTestScheduled bool
|
|
39
|
-
var
|
|
40
|
-
var tnTestColor bool
|
|
41
|
-
var tnTestStart time.Time
|
|
49
|
+
var tnTestStart float64
|
|
42
50
|
var tnTestCounts = map[string]int{}
|
|
43
|
-
|
|
51
|
+
|
|
52
|
+
// Hooks of the optional parts: a run() child sends its events to the parent;
|
|
53
|
+
// coverage and snapshots finish when the run does
|
|
54
|
+
var tnTestChildSink func(e *TnTestEvent)
|
|
55
|
+
var tnTestCoverage func(cfg *tnTestConfig) *TnCoverageSummary
|
|
56
|
+
var tnTestSnapshotsDone func()
|
|
44
57
|
|
|
45
58
|
// TestContext (t)
|
|
46
59
|
type TnTestContext struct {
|
|
@@ -62,7 +75,14 @@ func (t *TnTestContext) Get_fullName() string {
|
|
|
62
75
|
}
|
|
63
76
|
func (t *TnTestContext) Get_mock() *TnMockTracker { return t.mock }
|
|
64
77
|
func (t *TnTestContext) Get_signal() *TnAbortSignal { return t.signal }
|
|
78
|
+
|
|
79
|
+
// The source file the test was declared in
|
|
65
80
|
func (t *TnTestContext) Get_filePath() string {
|
|
81
|
+
for n := t.node; n != nil; n = n.parent {
|
|
82
|
+
if n.file != "" {
|
|
83
|
+
return n.file
|
|
84
|
+
}
|
|
85
|
+
}
|
|
66
86
|
if len(os.Args) > 0 {
|
|
67
87
|
return os.Args[0]
|
|
68
88
|
}
|
|
@@ -95,8 +115,9 @@ func (t *TnTestContext) plan(n float64, options ...interface{}) {
|
|
|
95
115
|
func (t *TnTestContext) runOnly(flag bool) {}
|
|
96
116
|
|
|
97
117
|
// t.test(name, [options], fn): runs the subtest now; resolves when it is done
|
|
98
|
-
func (t *TnTestContext) test(args ...interface{}) chan struct{} {
|
|
118
|
+
func (t *TnTestContext) test(file string, line float64, column float64, args ...interface{}) chan struct{} {
|
|
99
119
|
child := tnTestNew(false, args)
|
|
120
|
+
tnTestLocate(child, file, line, column)
|
|
100
121
|
child.parent = t.node
|
|
101
122
|
t.node.children = append(t.node.children, child)
|
|
102
123
|
t.asserts++
|
|
@@ -130,26 +151,37 @@ func tnTestNew(suite bool, args []interface{}) *TnTestNode {
|
|
|
130
151
|
n.fn = a
|
|
131
152
|
continue
|
|
132
153
|
}
|
|
133
|
-
switch s :=
|
|
154
|
+
switch s := TnGetValue(a, "skip").(type) {
|
|
134
155
|
case bool:
|
|
135
156
|
n.skip = s
|
|
136
157
|
case string:
|
|
137
158
|
n.skip, n.skipMsg = true, s
|
|
138
159
|
}
|
|
139
|
-
switch td :=
|
|
160
|
+
switch td := TnGetValue(a, "todo").(type) {
|
|
140
161
|
case bool:
|
|
141
162
|
n.todo = td
|
|
142
163
|
case string:
|
|
143
164
|
n.todo, n.todoMsg = true, td
|
|
144
165
|
}
|
|
145
|
-
if ms, ok :=
|
|
166
|
+
if ms, ok := TnGetValue(a, "timeout").(float64); ok {
|
|
146
167
|
n.timeoutMs = ms
|
|
147
168
|
}
|
|
169
|
+
if only, ok := TnGetValue(a, "only").(bool); ok {
|
|
170
|
+
n.only = only
|
|
171
|
+
}
|
|
148
172
|
}
|
|
149
173
|
}
|
|
150
174
|
return n
|
|
151
175
|
}
|
|
152
176
|
|
|
177
|
+
// The call site (a source path with forward slashes, as the compiler knows it)
|
|
178
|
+
func tnTestLocate(n *TnTestNode, file string, line float64, column float64) {
|
|
179
|
+
if file != "" {
|
|
180
|
+
n.file = filepath.FromSlash(file)
|
|
181
|
+
}
|
|
182
|
+
n.line, n.column = line, column
|
|
183
|
+
}
|
|
184
|
+
|
|
153
185
|
func tnTestIsFunc(v interface{}) bool {
|
|
154
186
|
rv := reflect.ValueOf(v)
|
|
155
187
|
return rv.IsValid() && rv.Kind() == reflect.Func && !rv.IsNil()
|
|
@@ -164,16 +196,24 @@ func tnTestDepth(n *TnTestNode) int {
|
|
|
164
196
|
}
|
|
165
197
|
|
|
166
198
|
// test() / it(): registered with the enclosing describe (or the running test)
|
|
167
|
-
func TnTestRegister(variant string, args ...interface{}) chan struct{} {
|
|
199
|
+
func TnTestRegister(variant string, file string, line float64, column float64, args ...interface{}) chan struct{} {
|
|
168
200
|
n := tnTestNew(false, args)
|
|
201
|
+
tnTestLocate(n, file, line, column)
|
|
169
202
|
switch variant {
|
|
170
203
|
case "skip":
|
|
171
204
|
n.skip = true
|
|
172
205
|
case "todo":
|
|
173
206
|
n.todo = true
|
|
207
|
+
case "only":
|
|
208
|
+
n.only = true
|
|
174
209
|
}
|
|
175
210
|
if tnTestRunning != nil && tnTestCurrent == tnTestRoot && tnTestRunning.ctx != nil {
|
|
176
211
|
n.parent = tnTestRunning
|
|
212
|
+
// subtests go through the same filters
|
|
213
|
+
if tnTestFilters().active() && !tnTestSelected(n) {
|
|
214
|
+
n.done <- struct{}{}
|
|
215
|
+
return n.done
|
|
216
|
+
}
|
|
177
217
|
tnTestRunning.children = append(tnTestRunning.children, n)
|
|
178
218
|
tnRunTestNode(n, tnTestDepth(n))
|
|
179
219
|
return n.done
|
|
@@ -185,13 +225,16 @@ func TnTestRegister(variant string, args ...interface{}) chan struct{} {
|
|
|
185
225
|
}
|
|
186
226
|
|
|
187
227
|
// describe() / suite(): the body runs now and registers the suite's tests
|
|
188
|
-
func TnSuiteRegister(variant string, args ...interface{}) chan struct{} {
|
|
228
|
+
func TnSuiteRegister(variant string, file string, line float64, column float64, args ...interface{}) chan struct{} {
|
|
189
229
|
n := tnTestNew(true, args)
|
|
230
|
+
tnTestLocate(n, file, line, column)
|
|
190
231
|
switch variant {
|
|
191
232
|
case "skip":
|
|
192
233
|
n.skip = true
|
|
193
234
|
case "todo":
|
|
194
235
|
n.todo = true
|
|
236
|
+
case "only":
|
|
237
|
+
n.only = true
|
|
195
238
|
}
|
|
196
239
|
n.parent = tnTestCurrent
|
|
197
240
|
tnTestCurrent.children = append(tnTestCurrent.children, n)
|
|
@@ -232,12 +275,14 @@ func TnTestHook(kind string, fn interface{}, options ...interface{}) {
|
|
|
232
275
|
tnTestSchedule()
|
|
233
276
|
}
|
|
234
277
|
|
|
235
|
-
// The run starts once the main module has finished (a loop task)
|
|
278
|
+
// The run starts once the main module has finished (a loop task); the
|
|
279
|
+
// reporters are set up by the first test(), as Node does
|
|
236
280
|
func tnTestSchedule() {
|
|
237
281
|
if tnTestScheduled {
|
|
238
282
|
return
|
|
239
283
|
}
|
|
240
284
|
tnTestScheduled = true
|
|
285
|
+
tnTestOpenReporters()
|
|
241
286
|
TnLoopRef()
|
|
242
287
|
TnLoopPost(func() {
|
|
243
288
|
TnStartAsync(func() {
|
|
@@ -265,7 +310,7 @@ func tnTestInvoke(fn interface{}, ctx *TnTestContext, timeoutMs float64) tnTestO
|
|
|
265
310
|
if timeoutMs > 0 && !math.IsInf(timeoutMs, 1) {
|
|
266
311
|
ms := timeoutMs
|
|
267
312
|
timer := time.AfterFunc(time.Duration(ms*float64(time.Millisecond)), func() {
|
|
268
|
-
settle(tnTestOutcome{err:
|
|
313
|
+
settle(tnTestOutcome{err: "test timed out after " + strconv.FormatFloat(ms, 'f', -1, 64) + "ms", failed: true, timedOut: true})
|
|
269
314
|
})
|
|
270
315
|
defer timer.Stop()
|
|
271
316
|
}
|
|
@@ -340,14 +385,314 @@ func tnTestAncestors(n *TnTestNode) []*TnTestNode {
|
|
|
340
385
|
return chain
|
|
341
386
|
}
|
|
342
387
|
|
|
388
|
+
// The test runner's flags, from NODE_OPTIONS as Node reads them:
|
|
389
|
+
// --test-name-pattern / --test-skip-pattern (repeatable, "/re/flags" or a
|
|
390
|
+
// plain regular expression), --test-only, --test-reporter /
|
|
391
|
+
// --test-reporter-destination (repeatable, in pairs),
|
|
392
|
+
// --test-update-snapshots and the coverage flags
|
|
393
|
+
type tnTestConfig struct {
|
|
394
|
+
names []*regexp.Regexp
|
|
395
|
+
skips []*regexp.Regexp
|
|
396
|
+
only bool
|
|
397
|
+
reporters []string
|
|
398
|
+
destinations []string
|
|
399
|
+
updateSnapshots bool
|
|
400
|
+
coverage bool
|
|
401
|
+
coverageInclude []string
|
|
402
|
+
coverageExclude []string
|
|
403
|
+
// --test-coverage-lines / -branches / -functions (0: no threshold)
|
|
404
|
+
thresholds map[string]float64
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
func (c *tnTestConfig) active() bool { return len(c.names) > 0 || len(c.skips) > 0 || c.only }
|
|
408
|
+
|
|
409
|
+
var tnTestCfg *tnTestConfig
|
|
410
|
+
|
|
411
|
+
// NODE_OPTIONS words: spaces separate them, double quotes group
|
|
412
|
+
func tnNodeOptionWords(s string) []string {
|
|
413
|
+
words := []string{}
|
|
414
|
+
cur := strings.Builder{}
|
|
415
|
+
inQuote, has := false, false
|
|
416
|
+
for _, r := range s {
|
|
417
|
+
switch {
|
|
418
|
+
case r == '"':
|
|
419
|
+
inQuote = !inQuote
|
|
420
|
+
has = true
|
|
421
|
+
case r == ' ' && !inQuote:
|
|
422
|
+
if has {
|
|
423
|
+
words = append(words, cur.String())
|
|
424
|
+
cur.Reset()
|
|
425
|
+
has = false
|
|
426
|
+
}
|
|
427
|
+
default:
|
|
428
|
+
cur.WriteRune(r)
|
|
429
|
+
has = true
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
if has {
|
|
433
|
+
words = append(words, cur.String())
|
|
434
|
+
}
|
|
435
|
+
return words
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
func tnTestPattern(s string) *regexp.Regexp {
|
|
439
|
+
if len(s) > 2 && s[0] == '/' {
|
|
440
|
+
if i := strings.LastIndexByte(s, '/'); i > 0 {
|
|
441
|
+
prefix := ""
|
|
442
|
+
if strings.Contains(s[i+1:], "i") {
|
|
443
|
+
prefix = "(?i)"
|
|
444
|
+
}
|
|
445
|
+
if re, err := regexp.Compile(prefix + s[1:i]); err == nil {
|
|
446
|
+
return re
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
re, err := regexp.Compile(s)
|
|
451
|
+
if err != nil {
|
|
452
|
+
return regexp.MustCompile(regexp.QuoteMeta(s))
|
|
453
|
+
}
|
|
454
|
+
return re
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
var tnTestValueFlags = map[string]bool{
|
|
458
|
+
"--test-name-pattern": true, "--test-skip-pattern": true, "--test-reporter": true, "--test-reporter-destination": true,
|
|
459
|
+
"--test-coverage-include": true, "--test-coverage-exclude": true,
|
|
460
|
+
"--test-coverage-lines": true, "--test-coverage-branches": true, "--test-coverage-functions": true,
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
func tnTestFilters() *tnTestConfig {
|
|
464
|
+
if tnTestCfg != nil {
|
|
465
|
+
return tnTestCfg
|
|
466
|
+
}
|
|
467
|
+
c := &tnTestConfig{thresholds: map[string]float64{}}
|
|
468
|
+
words := tnNodeOptionWords(os.Getenv("NODE_OPTIONS"))
|
|
469
|
+
for i := 0; i < len(words); i++ {
|
|
470
|
+
name, value, hasValue := strings.Cut(words[i], "=")
|
|
471
|
+
if tnTestValueFlags[name] && !hasValue && i+1 < len(words) {
|
|
472
|
+
i++
|
|
473
|
+
value = words[i]
|
|
474
|
+
}
|
|
475
|
+
switch name {
|
|
476
|
+
case "--test-name-pattern":
|
|
477
|
+
c.names = append(c.names, tnTestPattern(value))
|
|
478
|
+
case "--test-skip-pattern":
|
|
479
|
+
c.skips = append(c.skips, tnTestPattern(value))
|
|
480
|
+
case "--test-only":
|
|
481
|
+
c.only = true
|
|
482
|
+
case "--test-reporter":
|
|
483
|
+
c.reporters = append(c.reporters, value)
|
|
484
|
+
case "--test-reporter-destination":
|
|
485
|
+
c.destinations = append(c.destinations, value)
|
|
486
|
+
case "--test-update-snapshots":
|
|
487
|
+
c.updateSnapshots = true
|
|
488
|
+
case "--experimental-test-coverage":
|
|
489
|
+
c.coverage = true
|
|
490
|
+
case "--test-coverage-include":
|
|
491
|
+
c.coverageInclude = append(c.coverageInclude, value)
|
|
492
|
+
case "--test-coverage-exclude":
|
|
493
|
+
c.coverageExclude = append(c.coverageExclude, value)
|
|
494
|
+
case "--test-coverage-lines", "--test-coverage-branches", "--test-coverage-functions":
|
|
495
|
+
if n, err := strconv.ParseFloat(value, 64); err == nil {
|
|
496
|
+
c.thresholds[strings.TrimPrefix(name, "--test-coverage-")] = n
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
tnTestCfg = c
|
|
501
|
+
return c
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
// A pattern matches a test's name, an ancestor's, or the names joined with spaces
|
|
505
|
+
func tnTestNameMatches(n *TnTestNode, patterns []*regexp.Regexp) bool {
|
|
506
|
+
names := []string{}
|
|
507
|
+
for p := n; p != nil && p.parent != nil; p = p.parent {
|
|
508
|
+
for _, re := range patterns {
|
|
509
|
+
if re.MatchString(p.name) {
|
|
510
|
+
return true
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
names = append([]string{p.name}, names...)
|
|
514
|
+
}
|
|
515
|
+
full := strings.Join(names, " ")
|
|
516
|
+
for _, re := range patterns {
|
|
517
|
+
if re.MatchString(full) {
|
|
518
|
+
return true
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
return false
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
func tnTestSelected(n *TnTestNode) bool {
|
|
525
|
+
c := tnTestFilters()
|
|
526
|
+
if len(c.names) > 0 && !tnTestNameMatches(n, c.names) {
|
|
527
|
+
return false
|
|
528
|
+
}
|
|
529
|
+
if len(c.skips) > 0 && tnTestNameMatches(n, c.skips) {
|
|
530
|
+
return false
|
|
531
|
+
}
|
|
532
|
+
if c.only {
|
|
533
|
+
for p := n; p != nil; p = p.parent {
|
|
534
|
+
if p.only {
|
|
535
|
+
return true
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
return false
|
|
539
|
+
}
|
|
540
|
+
return true
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
// Drops the tests the filters leave out, and the suites left empty (Node
|
|
544
|
+
// does not report them)
|
|
545
|
+
func tnTestPrune(n *TnTestNode) bool {
|
|
546
|
+
kept := []*TnTestNode{}
|
|
547
|
+
for _, child := range n.children {
|
|
548
|
+
if child.suite {
|
|
549
|
+
if tnTestPrune(child) {
|
|
550
|
+
kept = append(kept, child)
|
|
551
|
+
}
|
|
552
|
+
} else if tnTestSelected(child) {
|
|
553
|
+
kept = append(kept, child)
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
n.children = kept
|
|
557
|
+
return len(kept) > 0
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
// --- reporting ---
|
|
561
|
+
|
|
562
|
+
// A reporter and where its text goes
|
|
563
|
+
type tnTestSink struct {
|
|
564
|
+
reporter tnTestReporter
|
|
565
|
+
out *os.File
|
|
566
|
+
file bool
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
func (s *tnTestSink) write(text string) {
|
|
570
|
+
if text != "" {
|
|
571
|
+
s.out.WriteString(text)
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
var tnTestSinks []*tnTestSink
|
|
576
|
+
|
|
577
|
+
// --test-reporter / --test-reporter-destination, paired as Node pairs them
|
|
578
|
+
// (the default: spec on a terminal, else tap, to stdout)
|
|
579
|
+
func tnTestOpenReporters() {
|
|
580
|
+
if tnTestChildSink != nil {
|
|
581
|
+
return
|
|
582
|
+
}
|
|
583
|
+
cfg := tnTestFilters()
|
|
584
|
+
reporters, destinations := cfg.reporters, cfg.destinations
|
|
585
|
+
if len(reporters) == 0 && len(destinations) == 0 {
|
|
586
|
+
reporters = []string{"tap"}
|
|
587
|
+
if TnIsTTY(os.Stdout) {
|
|
588
|
+
reporters = []string{"spec"}
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
if len(reporters) == 1 && len(destinations) == 0 {
|
|
592
|
+
destinations = []string{"stdout"}
|
|
593
|
+
}
|
|
594
|
+
if len(destinations) != len(reporters) {
|
|
595
|
+
received := make([]interface{}, len(reporters))
|
|
596
|
+
for i, r := range reporters {
|
|
597
|
+
received[i] = r
|
|
598
|
+
}
|
|
599
|
+
panic(TnNodeError("TypeError", "ERR_INVALID_ARG_VALUE", "The argument '--test-reporter' must match the number of specified '--test-reporter-destination'. Received "+TnInspect(received)))
|
|
600
|
+
}
|
|
601
|
+
for i, name := range reporters {
|
|
602
|
+
sink := &tnTestSink{}
|
|
603
|
+
colors := tnTestColorsOn()
|
|
604
|
+
switch destinations[i] {
|
|
605
|
+
case "stdout":
|
|
606
|
+
sink.out = os.Stdout
|
|
607
|
+
case "stderr":
|
|
608
|
+
sink.out = os.Stderr
|
|
609
|
+
default:
|
|
610
|
+
path := destinations[i]
|
|
611
|
+
os.MkdirAll(filepath.Dir(path), 0o755)
|
|
612
|
+
f, err := os.Create(path)
|
|
613
|
+
if err != nil {
|
|
614
|
+
panic(TnNodeError("Error", "ENOENT", err.Error()))
|
|
615
|
+
}
|
|
616
|
+
sink.out, sink.file, colors = f, true, false
|
|
617
|
+
}
|
|
618
|
+
sink.reporter = tnNewTestReporter(name, colors)
|
|
619
|
+
if sink.reporter == nil {
|
|
620
|
+
cwd, _ := os.Getwd()
|
|
621
|
+
panic(TnNodeError("Error", "ERR_MODULE_NOT_FOUND", "Cannot find package '"+name+"' imported from "+cwd))
|
|
622
|
+
}
|
|
623
|
+
tnTestSinks = append(tnTestSinks, sink)
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
func tnTestEmit(e *TnTestEvent) {
|
|
628
|
+
if tnTestChildSink != nil {
|
|
629
|
+
tnTestChildSink(e)
|
|
630
|
+
return
|
|
631
|
+
}
|
|
632
|
+
for _, s := range tnTestSinks {
|
|
633
|
+
s.write(s.reporter.report(e))
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
func tnTestNumber(n *TnTestNode) int {
|
|
638
|
+
if n.parent != nil {
|
|
639
|
+
for i, c := range n.parent.children {
|
|
640
|
+
if c == n {
|
|
641
|
+
return i + 1
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
return 1
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
func tnTestEventFor(kind string, n *TnTestNode, depth int) *TnTestEvent {
|
|
649
|
+
d := &TnTestEventData{name: n.name, nesting: float64(depth), file: n.file, line: n.line, column: n.column}
|
|
650
|
+
if kind != "test:start" {
|
|
651
|
+
d.testNumber = float64(tnTestNumber(n))
|
|
652
|
+
kindName := "test"
|
|
653
|
+
if n.suite {
|
|
654
|
+
kindName = "suite"
|
|
655
|
+
}
|
|
656
|
+
d.details = &TnTestEventDetails{duration_ms: math.Round(n.duration*1e6) / 1e6, type_: kindName, passed: n.status != "fail"}
|
|
657
|
+
if n.err != nil {
|
|
658
|
+
if n.failure == nil {
|
|
659
|
+
n.failure = tnTestFailure(n.err, n.failureType)
|
|
660
|
+
}
|
|
661
|
+
d.details.error_ = n.failure
|
|
662
|
+
}
|
|
663
|
+
if n.skip {
|
|
664
|
+
d.skip = true
|
|
665
|
+
if n.skipMsg != "" {
|
|
666
|
+
d.skip = n.skipMsg
|
|
667
|
+
}
|
|
668
|
+
} else if n.todo {
|
|
669
|
+
d.todo = true
|
|
670
|
+
if n.todoMsg != "" {
|
|
671
|
+
d.todo = n.todoMsg
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
return &TnTestEvent{type_: kind, data: d}
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
func tnTestNote(nesting int, message string, level string, n *TnTestNode) *TnTestEvent {
|
|
679
|
+
d := &TnTestEventData{nesting: float64(nesting), message: message, level: level}
|
|
680
|
+
if n != nil {
|
|
681
|
+
d.file, d.line, d.column = n.file, n.line, n.column
|
|
682
|
+
}
|
|
683
|
+
return &TnTestEvent{type_: "test:diagnostic", data: d}
|
|
684
|
+
}
|
|
685
|
+
|
|
343
686
|
func tnRunRoot() {
|
|
344
|
-
tnTestStart =
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
fmt.Println("TAP version 13")
|
|
687
|
+
tnTestStart = tnTestNow()
|
|
688
|
+
cfg := tnTestFilters()
|
|
689
|
+
for _, s := range tnTestSinks {
|
|
690
|
+
s.write(s.reporter.begin())
|
|
349
691
|
}
|
|
350
692
|
root := tnTestRoot
|
|
693
|
+
if cfg.active() {
|
|
694
|
+
tnTestPrune(root)
|
|
695
|
+
}
|
|
351
696
|
ctx := &TnTestContext{node: root}
|
|
352
697
|
failedHook := tnRunHooks(root.before, ctx)
|
|
353
698
|
for i := 0; i < len(root.children); i++ {
|
|
@@ -363,12 +708,12 @@ func tnRunRoot() {
|
|
|
363
708
|
|
|
364
709
|
func tnRunTestNode(n *TnTestNode, depth int) {
|
|
365
710
|
n.started = true
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
if !
|
|
369
|
-
|
|
711
|
+
tnTestEmit(tnTestEventFor("test:start", n, depth))
|
|
712
|
+
// .only without --test-only: Node's notice, as the test's diagnostic
|
|
713
|
+
if n.only && !tnTestFilters().only {
|
|
714
|
+
n.diagnostics = append(n.diagnostics, "'only' and 'runOnly' require the --test-only command-line option.")
|
|
370
715
|
}
|
|
371
|
-
start :=
|
|
716
|
+
start := tnTestNow()
|
|
372
717
|
ctx := &TnTestContext{node: n, mock: TnNewMockTracker(), signal: TnNewAbortSignal()}
|
|
373
718
|
n.ctx = ctx
|
|
374
719
|
defer func() {
|
|
@@ -430,12 +775,12 @@ func tnRunTestNode(n *TnTestNode, depth int) {
|
|
|
430
775
|
n.failureType = "testCodeFailure"
|
|
431
776
|
}
|
|
432
777
|
} else if ctx.planSet && ctx.planned != ctx.asserts {
|
|
433
|
-
n.err =
|
|
778
|
+
n.err = "plan expected " + strconv.FormatFloat(ctx.planned, 'f', -1, 64) + " assertions but received " + strconv.FormatFloat(ctx.asserts, 'f', -1, 64)
|
|
434
779
|
n.failureType = "testCodeFailure"
|
|
435
780
|
}
|
|
436
781
|
tnTestRunning = prev
|
|
437
782
|
}
|
|
438
|
-
n.duration =
|
|
783
|
+
n.duration = tnTestNow() - start
|
|
439
784
|
switch {
|
|
440
785
|
case n.skip:
|
|
441
786
|
n.status = "skip"
|
|
@@ -445,23 +790,20 @@ func tnRunTestNode(n *TnTestNode, depth int) {
|
|
|
445
790
|
n.status = "fail"
|
|
446
791
|
default:
|
|
447
792
|
n.status = "pass"
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
failed
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
noun = "subtests"
|
|
460
|
-
}
|
|
461
|
-
n.err = TnNodeError("Error", "ERR_TEST_FAILURE", strconv.Itoa(failed)+" "+noun+" failed")
|
|
462
|
-
n.failureType = "subtestsFailed"
|
|
463
|
-
break
|
|
793
|
+
failed := 0
|
|
794
|
+
for _, c := range n.children {
|
|
795
|
+
if c.status == "fail" {
|
|
796
|
+
failed++
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
if failed > 0 {
|
|
800
|
+
n.status = "fail"
|
|
801
|
+
noun := "subtest"
|
|
802
|
+
if failed > 1 {
|
|
803
|
+
noun = "subtests"
|
|
464
804
|
}
|
|
805
|
+
n.err = strconv.Itoa(failed) + " " + noun + " failed"
|
|
806
|
+
n.failureType = "subtestsFailed"
|
|
465
807
|
}
|
|
466
808
|
}
|
|
467
809
|
if n.suite {
|
|
@@ -477,9 +819,6 @@ func tnRunTestNode(n *TnTestNode, depth int) {
|
|
|
477
819
|
} else {
|
|
478
820
|
tnTestCounts["fail"]++
|
|
479
821
|
}
|
|
480
|
-
if n.failureType != "subtestsFailed" {
|
|
481
|
-
tnTestFailures = append(tnTestFailures, n)
|
|
482
|
-
}
|
|
483
822
|
case "skip":
|
|
484
823
|
tnTestCounts["skipped"]++
|
|
485
824
|
case "todo":
|
|
@@ -489,201 +828,69 @@ func tnRunTestNode(n *TnTestNode, depth int) {
|
|
|
489
828
|
tnTestReport(n, depth)
|
|
490
829
|
}
|
|
491
830
|
|
|
492
|
-
//
|
|
493
|
-
func
|
|
494
|
-
|
|
495
|
-
|
|
831
|
+
// A finished test's events: complete, its subtests' plan, pass / fail, diagnostics
|
|
832
|
+
func tnTestReport(n *TnTestNode, depth int) {
|
|
833
|
+
tnTestEmit(tnTestEventFor("test:complete", n, depth))
|
|
834
|
+
if len(n.children) > 0 {
|
|
835
|
+
tnTestEmit(&TnTestEvent{type_: "test:plan", data: &TnTestEventData{nesting: float64(depth + 1), count: float64(len(n.children)), file: n.file, line: n.line, column: n.column}})
|
|
496
836
|
}
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
continue
|
|
501
|
-
}
|
|
502
|
-
p.headerShown = true
|
|
503
|
-
fmt.Println(strings.Repeat(" ", i-1) + "\\u25b6 " + p.name)
|
|
837
|
+
kind := "test:pass"
|
|
838
|
+
if n.err != nil && !n.skip {
|
|
839
|
+
kind = "test:fail"
|
|
504
840
|
}
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
if !tnTestColor {
|
|
509
|
-
return s
|
|
841
|
+
tnTestEmit(tnTestEventFor(kind, n, depth))
|
|
842
|
+
for _, d := range n.diagnostics {
|
|
843
|
+
tnTestEmit(tnTestNote(depth, d, "info", n))
|
|
510
844
|
}
|
|
511
|
-
return "\\x1b[" + code + "m" + s + "\\x1b[39m"
|
|
512
845
|
}
|
|
513
846
|
|
|
514
847
|
func tnTestDuration(ms float64) string {
|
|
515
848
|
return strconv.FormatFloat(math.Round(ms*1e6)/1e6, 'f', -1, 64)
|
|
516
849
|
}
|
|
517
850
|
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
}
|
|
528
|
-
if key == "name" {
|
|
529
|
-
return e.Get_name()
|
|
530
|
-
}
|
|
531
|
-
return e.prop(key)
|
|
532
|
-
}
|
|
533
|
-
if key == "message" {
|
|
534
|
-
return TnJSString(err)
|
|
535
|
-
}
|
|
536
|
-
return nil
|
|
537
|
-
}
|
|
538
|
-
|
|
539
|
-
func tnTestYamlString(s string, indent string) string {
|
|
540
|
-
if strings.Contains(s, "\\n") {
|
|
541
|
-
lines := strings.Split(s, "\\n")
|
|
542
|
-
for i, l := range lines {
|
|
543
|
-
lines[i] = indent + " " + l
|
|
544
|
-
}
|
|
545
|
-
return "|-\\n" + strings.Join(lines, "\\n")
|
|
546
|
-
}
|
|
547
|
-
return "'" + strings.ReplaceAll(s, "'", "''") + "'"
|
|
548
|
-
}
|
|
549
|
-
|
|
550
|
-
func tnTestReport(n *TnTestNode, depth int) {
|
|
551
|
-
if tnTestSpec {
|
|
552
|
-
tnTestReportSpec(n, depth)
|
|
553
|
-
return
|
|
554
|
-
}
|
|
555
|
-
indent := strings.Repeat(" ", depth)
|
|
556
|
-
if len(n.children) > 0 {
|
|
557
|
-
fmt.Println(indent + " 1.." + strconv.Itoa(len(n.children)))
|
|
558
|
-
}
|
|
559
|
-
number := 1
|
|
560
|
-
if n.parent != nil {
|
|
561
|
-
for i, c := range n.parent.children {
|
|
562
|
-
if c == n {
|
|
563
|
-
number = i + 1
|
|
564
|
-
}
|
|
565
|
-
}
|
|
566
|
-
}
|
|
567
|
-
line := "ok"
|
|
568
|
-
if n.status == "fail" {
|
|
569
|
-
line = "not ok"
|
|
570
|
-
}
|
|
571
|
-
line = indent + line + " " + strconv.Itoa(number) + " - " + n.name
|
|
572
|
-
switch n.status {
|
|
573
|
-
case "skip":
|
|
574
|
-
line += " # SKIP"
|
|
575
|
-
if n.skipMsg != "" {
|
|
576
|
-
line += " " + n.skipMsg
|
|
577
|
-
}
|
|
578
|
-
case "todo":
|
|
579
|
-
line += " # TODO"
|
|
580
|
-
if n.todoMsg != "" {
|
|
581
|
-
line += " " + n.todoMsg
|
|
851
|
+
// The root's report: the plan, the counters as diagnostics, coverage, the summary
|
|
852
|
+
func tnTestFinish() {
|
|
853
|
+
cfg := tnTestFilters()
|
|
854
|
+
duration := tnTestNow() - tnTestStart
|
|
855
|
+
stats := []string{"tests", "suites", "pass", "fail", "cancelled", "skipped", "todo"}
|
|
856
|
+
if tnTestChildSink == nil {
|
|
857
|
+
tnTestEmit(&TnTestEvent{type_: "test:plan", data: &TnTestEventData{count: float64(len(tnTestRoot.children))}})
|
|
858
|
+
for _, key := range stats {
|
|
859
|
+
tnTestEmit(tnTestNote(0, key+" "+strconv.Itoa(tnTestCounts[key]), "info", nil))
|
|
582
860
|
}
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
if e, ok := n.err.(*TnError); ok && e.Get_name() == "AssertionError" && !strings.HasSuffix(msg, "\\n") && strings.Contains(msg, "\\n") {
|
|
596
|
-
msg += "\\n"
|
|
597
|
-
}
|
|
598
|
-
fmt.Println(indent + " error: " + tnTestYamlString(msg, indent))
|
|
599
|
-
code, _ := tnTestErrorField(n.err, "code").(string)
|
|
600
|
-
if code == "" {
|
|
601
|
-
code = "ERR_TEST_FAILURE"
|
|
602
|
-
}
|
|
603
|
-
fmt.Println(indent + " code: '" + code + "'")
|
|
604
|
-
if e, isTnErr := n.err.(*TnError); isTnErr && n.failureType == "testCodeFailure" && e.Get_name() == "AssertionError" {
|
|
605
|
-
fmt.Println(indent + " name: '" + TnJSString(tnTestErrorField(n.err, "name")) + "'")
|
|
606
|
-
for _, key := range []string{"expected", "actual", "operator"} {
|
|
607
|
-
if e, ok := n.err.(*TnError); ok {
|
|
608
|
-
if v, has := e.props[key]; has {
|
|
609
|
-
fmt.Println(indent + " " + key + ": " + TnInspect(v))
|
|
610
|
-
}
|
|
861
|
+
tnTestEmit(tnTestNote(0, "duration_ms "+tnTestDuration(duration), "info", nil))
|
|
862
|
+
}
|
|
863
|
+
success := tnTestCounts["fail"] == 0 && tnTestCounts["cancelled"] == 0
|
|
864
|
+
if cfg.coverage && tnTestCoverage != nil {
|
|
865
|
+
if summary := tnTestCoverage(cfg); summary != nil {
|
|
866
|
+
totals := map[string]float64{"line": summary.totals.coveredLinePercent, "branch": summary.totals.coveredBranchPercent, "function": summary.totals.coveredFunctionPercent}
|
|
867
|
+
for _, kind := range []string{"lines", "branches", "functions"} {
|
|
868
|
+
threshold, set := cfg.thresholds[kind]
|
|
869
|
+
name := map[string]string{"lines": "line", "branches": "branch", "functions": "function"}[kind]
|
|
870
|
+
if set && totals[name] < threshold {
|
|
871
|
+
success = false
|
|
872
|
+
tnTestEmit(tnTestNote(0, "Error: "+strconv.FormatFloat(totals[name], 'f', 2, 64)+"% "+name+" coverage does not meet threshold of "+TnNumStr(threshold)+"%.", "error", nil))
|
|
611
873
|
}
|
|
612
874
|
}
|
|
875
|
+
tnTestEmit(&TnTestEvent{type_: "test:coverage", data: &TnTestEventData{summary: summary}})
|
|
613
876
|
}
|
|
614
877
|
}
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
fmt.Println(indent + "# " + d)
|
|
878
|
+
if tnTestSnapshotsDone != nil {
|
|
879
|
+
tnTestSnapshotsDone()
|
|
618
880
|
}
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
dur := " (" + tnTestDuration(n.duration) + "ms)"
|
|
624
|
-
var line string
|
|
625
|
-
switch n.status {
|
|
626
|
-
case "pass":
|
|
627
|
-
line = tnTestColorize("32", "\\u2714 "+n.name+dur)
|
|
628
|
-
case "fail":
|
|
629
|
-
line = tnTestColorize("31", "\\u2716 "+n.name+dur)
|
|
630
|
-
case "skip":
|
|
631
|
-
msg := "SKIP"
|
|
632
|
-
if n.skipMsg != "" {
|
|
633
|
-
msg = n.skipMsg
|
|
634
|
-
}
|
|
635
|
-
line = tnTestColorize("90", "\\ufe63 "+n.name+dur+" # "+msg)
|
|
636
|
-
case "todo":
|
|
637
|
-
msg := "TODO"
|
|
638
|
-
if n.todoMsg != "" {
|
|
639
|
-
msg = n.todoMsg
|
|
640
|
-
}
|
|
641
|
-
mark := "\\u2714 "
|
|
642
|
-
if n.err != nil {
|
|
643
|
-
mark = "\\u2716 "
|
|
644
|
-
}
|
|
645
|
-
line = tnTestColorize("33", mark+n.name+dur+" # "+msg)
|
|
646
|
-
}
|
|
647
|
-
fmt.Println(indent + line)
|
|
648
|
-
for _, d := range n.diagnostics {
|
|
649
|
-
fmt.Println(indent + tnTestColorize("34", "\\u2139 "+d))
|
|
881
|
+
counts := &TnTestCounts{
|
|
882
|
+
tests: float64(tnTestCounts["tests"]), failed: float64(tnTestCounts["fail"]), passed: float64(tnTestCounts["pass"]),
|
|
883
|
+
cancelled: float64(tnTestCounts["cancelled"]), skipped: float64(tnTestCounts["skipped"]), todo: float64(tnTestCounts["todo"]),
|
|
884
|
+
topLevel: float64(len(tnTestRoot.children)), suites: float64(tnTestCounts["suites"]),
|
|
650
885
|
}
|
|
651
|
-
}
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
fmt.Println("1.." + strconv.Itoa(len(tnTestRoot.children)))
|
|
657
|
-
}
|
|
658
|
-
for _, key := range stats {
|
|
659
|
-
line := key + " " + strconv.Itoa(tnTestCounts[key])
|
|
660
|
-
if tnTestSpec {
|
|
661
|
-
fmt.Println(tnTestColorize("34", "\\u2139 "+line))
|
|
662
|
-
} else {
|
|
663
|
-
fmt.Println("# " + line)
|
|
664
|
-
}
|
|
665
|
-
}
|
|
666
|
-
duration := "duration_ms " + tnTestDuration(float64(time.Since(tnTestStart).Nanoseconds())/1e6)
|
|
667
|
-
if tnTestSpec {
|
|
668
|
-
fmt.Println(tnTestColorize("34", "\\u2139 "+duration))
|
|
669
|
-
if len(tnTestFailures) > 0 {
|
|
670
|
-
fmt.Println("\\n" + tnTestColorize("31", "\\u2716 failing tests:"))
|
|
671
|
-
for _, n := range tnTestFailures {
|
|
672
|
-
fmt.Println("\\n" + tnTestColorize("31", "\\u2716 "+n.name+" ("+tnTestDuration(n.duration)+"ms)"))
|
|
673
|
-
name := TnJSString(tnTestErrorField(n.err, "name"))
|
|
674
|
-
head := name
|
|
675
|
-
if code, ok := tnTestErrorField(n.err, "code").(string); ok && code != "" {
|
|
676
|
-
head += " [" + code + "]"
|
|
677
|
-
}
|
|
678
|
-
for _, l := range strings.Split(head+": "+TnJSString(tnTestErrorField(n.err, "message")), "\\n") {
|
|
679
|
-
fmt.Println(" " + l)
|
|
680
|
-
}
|
|
681
|
-
}
|
|
886
|
+
tnTestEmit(&TnTestEvent{type_: "test:summary", data: &TnTestEventData{success: success, counts: counts, duration_ms: math.Round(duration*1e6) / 1e6}})
|
|
887
|
+
for _, s := range tnTestSinks {
|
|
888
|
+
s.write(s.reporter.end())
|
|
889
|
+
if s.file {
|
|
890
|
+
s.out.Close()
|
|
682
891
|
}
|
|
683
|
-
} else {
|
|
684
|
-
fmt.Println("# " + duration)
|
|
685
892
|
}
|
|
686
|
-
if
|
|
893
|
+
if !success {
|
|
687
894
|
TnSetExitCode(1)
|
|
688
895
|
}
|
|
689
896
|
}
|
|
@@ -704,6 +911,8 @@ type TnMockFunctionContext struct {
|
|
|
704
911
|
impl interface{}
|
|
705
912
|
original interface{}
|
|
706
913
|
once map[int]interface{}
|
|
914
|
+
// mock.method & co.: puts the original back where the mock was
|
|
915
|
+
unmock func()
|
|
707
916
|
}
|
|
708
917
|
|
|
709
918
|
func (m *TnMockFunctionContext) Get_calls() []*TnMockCall { return m.calls }
|
|
@@ -717,7 +926,13 @@ func (m *TnMockFunctionContext) mockImplementationOnce(fn interface{}, onCall ..
|
|
|
717
926
|
}
|
|
718
927
|
m.once[index] = fn
|
|
719
928
|
}
|
|
720
|
-
func (m *TnMockFunctionContext) restore() {
|
|
929
|
+
func (m *TnMockFunctionContext) restore() {
|
|
930
|
+
m.impl = m.original
|
|
931
|
+
if m.unmock != nil {
|
|
932
|
+
m.unmock()
|
|
933
|
+
m.unmock = nil
|
|
934
|
+
}
|
|
935
|
+
}
|
|
721
936
|
|
|
722
937
|
var tnMocks = map[uintptr]*TnMockFunctionContext{}
|
|
723
938
|
|
|
@@ -759,7 +974,7 @@ func (m *TnMockTimers) enable(options ...interface{}) {
|
|
|
759
974
|
apis := map[string]bool{"setTimeout": true, "setInterval": true, "setImmediate": true, "Date": true}
|
|
760
975
|
m.now = 0
|
|
761
976
|
if len(options) > 0 && options[0] != nil {
|
|
762
|
-
switch list :=
|
|
977
|
+
switch list := TnGetValue(options[0], "apis").(type) {
|
|
763
978
|
case []string:
|
|
764
979
|
apis = map[string]bool{}
|
|
765
980
|
for _, a := range list {
|
|
@@ -771,7 +986,7 @@ func (m *TnMockTimers) enable(options ...interface{}) {
|
|
|
771
986
|
apis[fmt.Sprint(a)] = true
|
|
772
987
|
}
|
|
773
988
|
}
|
|
774
|
-
switch now :=
|
|
989
|
+
switch now := TnGetValue(options[0], "now").(type) {
|
|
775
990
|
case float64:
|
|
776
991
|
m.now = now
|
|
777
992
|
case time.Time:
|
|
@@ -943,6 +1158,134 @@ func TnMockFnEmpty(tracker *TnMockTracker) func(...interface{}) interface{} {
|
|
|
943
1158
|
return TnMockFn(tracker, func(args ...interface{}) interface{} { return nil })
|
|
944
1159
|
}
|
|
945
1160
|
|
|
1161
|
+
// The implementation argument of mock.method / getter / setter (options objects aside)
|
|
1162
|
+
func tnMockImplementation(rest []interface{}) interface{} {
|
|
1163
|
+
for _, r := range rest {
|
|
1164
|
+
if tnTestIsFunc(r) {
|
|
1165
|
+
return r
|
|
1166
|
+
}
|
|
1167
|
+
}
|
|
1168
|
+
return nil
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1171
|
+
func tnMockNotAMethod(name string) *TnError {
|
|
1172
|
+
return TnNodeError("TypeError", "ERR_INVALID_ARG_VALUE", "The argument 'methodName' must be a method. Received '"+name+"'")
|
|
1173
|
+
}
|
|
1174
|
+
|
|
1175
|
+
// mock.method(object, name, implementation?) on a function-valued field (an
|
|
1176
|
+
// object literal's method or accessor): the field holds the mock until restore()
|
|
1177
|
+
func TnMockMethod[F any](tracker *TnMockTracker, name string, slot *F, rest ...interface{}) F {
|
|
1178
|
+
original := *slot
|
|
1179
|
+
if !tnTestIsFunc(original) {
|
|
1180
|
+
panic(tnMockNotAMethod(name))
|
|
1181
|
+
}
|
|
1182
|
+
fn := TnMockFn(tracker, original)
|
|
1183
|
+
ctx := tnMocks[tnFuncID(fn)]
|
|
1184
|
+
if impl := tnMockImplementation(rest); impl != nil {
|
|
1185
|
+
ctx.impl = impl
|
|
1186
|
+
}
|
|
1187
|
+
*slot = fn
|
|
1188
|
+
ctx.unmock = func() { *slot = original }
|
|
1189
|
+
return fn
|
|
1190
|
+
}
|
|
1191
|
+
|
|
1192
|
+
// mock.method on a class instance: the method's override slot holds the mock
|
|
1193
|
+
// (the method calls it while it is set); original is the method's own body
|
|
1194
|
+
func TnMockSlot[F any](tracker *TnMockTracker, slot *F, original F, rest ...interface{}) F {
|
|
1195
|
+
fn := TnMockFn(tracker, original)
|
|
1196
|
+
ctx := tnMocks[tnFuncID(fn)]
|
|
1197
|
+
if impl := tnMockImplementation(rest); impl != nil {
|
|
1198
|
+
ctx.impl = impl
|
|
1199
|
+
}
|
|
1200
|
+
*slot = fn
|
|
1201
|
+
ctx.unmock = func() {
|
|
1202
|
+
var none F
|
|
1203
|
+
*slot = none
|
|
1204
|
+
}
|
|
1205
|
+
return fn
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1208
|
+
// mock.method(console, 'log') & co.: calls of a namespace's function check
|
|
1209
|
+
// for a mock by name (TnMockedCall)
|
|
1210
|
+
var tnMockedGlobals = map[string]*TnMockFunctionContext{}
|
|
1211
|
+
|
|
1212
|
+
func TnMockGlobal(tracker *TnMockTracker, key string, rest ...interface{}) func(...interface{}) interface{} {
|
|
1213
|
+
ctx := &TnMockFunctionContext{once: map[int]interface{}{}, impl: tnMockImplementation(rest)}
|
|
1214
|
+
tracker.mocks = append(tracker.mocks, ctx)
|
|
1215
|
+
tnMockedGlobals[key] = ctx
|
|
1216
|
+
ctx.unmock = func() {
|
|
1217
|
+
if tnMockedGlobals[key] == ctx {
|
|
1218
|
+
delete(tnMockedGlobals, key)
|
|
1219
|
+
}
|
|
1220
|
+
}
|
|
1221
|
+
fn := func(args ...interface{}) interface{} {
|
|
1222
|
+
return tnMockedCallVia(ctx, args, func() interface{} { return nil })
|
|
1223
|
+
}
|
|
1224
|
+
tnMocks[tnFuncID(fn)] = ctx
|
|
1225
|
+
return fn
|
|
1226
|
+
}
|
|
1227
|
+
|
|
1228
|
+
func tnMockedCallVia(ctx *TnMockFunctionContext, args []interface{}, original func() interface{}) interface{} {
|
|
1229
|
+
call := &TnMockCall{arguments: args}
|
|
1230
|
+
impl := ctx.impl
|
|
1231
|
+
if once, ok := ctx.once[len(ctx.calls)]; ok {
|
|
1232
|
+
impl = once
|
|
1233
|
+
delete(ctx.once, len(ctx.calls))
|
|
1234
|
+
}
|
|
1235
|
+
ctx.calls = append(ctx.calls, call)
|
|
1236
|
+
defer func() {
|
|
1237
|
+
if r := recover(); r != nil {
|
|
1238
|
+
call.error_ = r
|
|
1239
|
+
panic(r)
|
|
1240
|
+
}
|
|
1241
|
+
}()
|
|
1242
|
+
var result interface{}
|
|
1243
|
+
if impl == nil {
|
|
1244
|
+
result = original()
|
|
1245
|
+
} else {
|
|
1246
|
+
result = TnInvoke(impl, args...)
|
|
1247
|
+
}
|
|
1248
|
+
call.result = result
|
|
1249
|
+
return result
|
|
1250
|
+
}
|
|
1251
|
+
|
|
1252
|
+
// A call of a namespace function that a mock may replace (args evaluate only then)
|
|
1253
|
+
func TnMockedCall[R any](key string, args func() []interface{}, original func() R) R {
|
|
1254
|
+
ctx, mocked := tnMockedGlobals[key]
|
|
1255
|
+
if !mocked {
|
|
1256
|
+
return original()
|
|
1257
|
+
}
|
|
1258
|
+
result := tnMockedCallVia(ctx, args(), func() interface{} { return original() })
|
|
1259
|
+
if r, ok := result.(R); ok {
|
|
1260
|
+
return r
|
|
1261
|
+
}
|
|
1262
|
+
var zero R
|
|
1263
|
+
if result == nil {
|
|
1264
|
+
return zero
|
|
1265
|
+
}
|
|
1266
|
+
return tnInvokeArg(result, reflect.TypeOf(&zero).Elem()).Interface().(R)
|
|
1267
|
+
}
|
|
1268
|
+
|
|
1269
|
+
func TnMockedCallVoid(key string, args func() []interface{}, original func()) {
|
|
1270
|
+
ctx, mocked := tnMockedGlobals[key]
|
|
1271
|
+
if !mocked {
|
|
1272
|
+
original()
|
|
1273
|
+
return
|
|
1274
|
+
}
|
|
1275
|
+
tnMockedCallVia(ctx, args(), func() interface{} {
|
|
1276
|
+
original()
|
|
1277
|
+
return nil
|
|
1278
|
+
})
|
|
1279
|
+
}
|
|
1280
|
+
|
|
1281
|
+
// console.log.mock: the record of a namespace function's mock
|
|
1282
|
+
func TnMockedContext(key string) *TnMockFunctionContext {
|
|
1283
|
+
if ctx, ok := tnMockedGlobals[key]; ok {
|
|
1284
|
+
return ctx
|
|
1285
|
+
}
|
|
1286
|
+
panic(TnNodeError("TypeError", "", "not a mock function"))
|
|
1287
|
+
}
|
|
1288
|
+
|
|
946
1289
|
// fn.mock: the call record of a mock function
|
|
947
1290
|
func TnMockOf(fn interface{}) *TnMockFunctionContext {
|
|
948
1291
|
if ctx, ok := tnMocks[tnFuncID(fn)]; ok {
|
|
@@ -966,20 +1309,252 @@ func (t *TnMockTracker) restoreAll() {
|
|
|
966
1309
|
m.restore()
|
|
967
1310
|
}
|
|
968
1311
|
}`;
|
|
1312
|
+
const snapshotHelper = `// node:test snapshots: t.assert.snapshot (one <test file>.snapshot file per
|
|
1313
|
+
// test file, Node's format) and t.assert.fileSnapshot; --test-update-snapshots
|
|
1314
|
+
// writes them, otherwise they are compared
|
|
1315
|
+
const tnSnapshotTip = "Missing snapshots can be generated by rerunning the command with the --test-update-snapshots flag."
|
|
1316
|
+
|
|
1317
|
+
type tnSnapshotFile struct {
|
|
1318
|
+
path string
|
|
1319
|
+
snapshots map[string]string
|
|
1320
|
+
counts map[string]int
|
|
1321
|
+
loaded bool
|
|
1322
|
+
}
|
|
1323
|
+
|
|
1324
|
+
var tnSnapshotFiles = map[string]*tnSnapshotFile{}
|
|
1325
|
+
var tnSnapshotOrder []*tnSnapshotFile
|
|
1326
|
+
var tnSnapshotSerializers []interface{}
|
|
1327
|
+
var tnSnapshotResolve interface{}
|
|
1328
|
+
|
|
1329
|
+
func init() {
|
|
1330
|
+
tnTestSnapshotsDone = tnSnapshotWriteAll
|
|
1331
|
+
}
|
|
1332
|
+
|
|
1333
|
+
// snapshot.setDefaultSnapshotSerializers / setResolveSnapshotPath
|
|
1334
|
+
func TnSetDefaultSnapshotSerializers(list interface{}) { tnSnapshotSerializers = tnTestList(list) }
|
|
1335
|
+
func TnSetResolveSnapshotPath(fn interface{}) { tnSnapshotResolve = fn }
|
|
1336
|
+
|
|
1337
|
+
func tnSnapshotEscape(s string) string {
|
|
1338
|
+
s = strings.ReplaceAll(s, "\\\\", "\\\\\\\\")
|
|
1339
|
+
s = strings.ReplaceAll(s, "\`", "\\\\\`")
|
|
1340
|
+
return strings.ReplaceAll(s, "\${", "\\\\\${")
|
|
1341
|
+
}
|
|
1342
|
+
|
|
1343
|
+
func tnSnapshotUnescape(s string) string {
|
|
1344
|
+
var b strings.Builder
|
|
1345
|
+
for i := 0; i < len(s); i++ {
|
|
1346
|
+
if s[i] == '\\\\' && i+1 < len(s) {
|
|
1347
|
+
i++
|
|
1348
|
+
}
|
|
1349
|
+
b.WriteByte(s[i])
|
|
1350
|
+
}
|
|
1351
|
+
return b.String()
|
|
1352
|
+
}
|
|
1353
|
+
|
|
1354
|
+
func tnSnapshotState(msg string, file string) *TnError {
|
|
1355
|
+
e := tnMockTimersState(msg)
|
|
1356
|
+
e.props["filename"] = file
|
|
1357
|
+
return e
|
|
1358
|
+
}
|
|
1359
|
+
|
|
1360
|
+
func tnSnapshotFor(testFile string) *tnSnapshotFile {
|
|
1361
|
+
if f, ok := tnSnapshotFiles[testFile]; ok {
|
|
1362
|
+
return f
|
|
1363
|
+
}
|
|
1364
|
+
path := testFile + ".snapshot"
|
|
1365
|
+
if tnSnapshotResolve != nil {
|
|
1366
|
+
resolved, ok := TnInvoke(tnSnapshotResolve, testFile).(string)
|
|
1367
|
+
if !ok {
|
|
1368
|
+
panic(tnSnapshotState("Invalid snapshot filename.", ""))
|
|
1369
|
+
}
|
|
1370
|
+
path = resolved
|
|
1371
|
+
}
|
|
1372
|
+
f := &tnSnapshotFile{path: path, snapshots: map[string]string{}, counts: map[string]int{}, loaded: tnTestFilters().updateSnapshots}
|
|
1373
|
+
tnSnapshotFiles[testFile] = f
|
|
1374
|
+
tnSnapshotOrder = append(tnSnapshotOrder, f)
|
|
1375
|
+
return f
|
|
1376
|
+
}
|
|
1377
|
+
|
|
1378
|
+
func (f *tnSnapshotFile) nextID(name string) string {
|
|
1379
|
+
count := f.counts[name]
|
|
1380
|
+
if count == 0 {
|
|
1381
|
+
count = 1
|
|
1382
|
+
}
|
|
1383
|
+
f.counts[name] = count + 1
|
|
1384
|
+
return name + " " + strconv.Itoa(count)
|
|
1385
|
+
}
|
|
1386
|
+
|
|
1387
|
+
// exports[\`name 1\`] = \`value\`; entries, keeping the values escaped as written
|
|
1388
|
+
func tnSnapshotParse(src string) (map[string]string, bool) {
|
|
1389
|
+
out := map[string]string{}
|
|
1390
|
+
i := 0
|
|
1391
|
+
literal := func() (string, bool) {
|
|
1392
|
+
start := i
|
|
1393
|
+
for i < len(src) {
|
|
1394
|
+
switch src[i] {
|
|
1395
|
+
case '\\\\':
|
|
1396
|
+
i += 2
|
|
1397
|
+
continue
|
|
1398
|
+
case '\`':
|
|
1399
|
+
text := src[start:i]
|
|
1400
|
+
i++
|
|
1401
|
+
return text, true
|
|
1402
|
+
}
|
|
1403
|
+
i++
|
|
1404
|
+
}
|
|
1405
|
+
return "", false
|
|
1406
|
+
}
|
|
1407
|
+
for {
|
|
1408
|
+
at := strings.Index(src[i:], "exports[\`")
|
|
1409
|
+
if at < 0 {
|
|
1410
|
+
return out, true
|
|
1411
|
+
}
|
|
1412
|
+
i += at + len("exports[\`")
|
|
1413
|
+
key, ok := literal()
|
|
1414
|
+
if !ok || !strings.HasPrefix(src[i:], "] = \`") {
|
|
1415
|
+
return nil, false
|
|
1416
|
+
}
|
|
1417
|
+
i += len("] = \`")
|
|
1418
|
+
value, ok := literal()
|
|
1419
|
+
if !ok {
|
|
1420
|
+
return nil, false
|
|
1421
|
+
}
|
|
1422
|
+
out[tnSnapshotUnescape(key)] = value
|
|
1423
|
+
}
|
|
1424
|
+
}
|
|
1425
|
+
|
|
1426
|
+
func (f *tnSnapshotFile) read() {
|
|
1427
|
+
if f.loaded {
|
|
1428
|
+
return
|
|
1429
|
+
}
|
|
1430
|
+
data, err := os.ReadFile(f.path)
|
|
1431
|
+
msg := "Cannot read snapshot file '" + f.path + ".'"
|
|
1432
|
+
if err != nil {
|
|
1433
|
+
if os.IsNotExist(err) {
|
|
1434
|
+
msg += " " + tnSnapshotTip
|
|
1435
|
+
}
|
|
1436
|
+
panic(tnSnapshotState(msg, f.path))
|
|
1437
|
+
}
|
|
1438
|
+
snapshots, ok := tnSnapshotParse(string(data))
|
|
1439
|
+
if !ok {
|
|
1440
|
+
panic(tnSnapshotState(msg, f.path))
|
|
1441
|
+
}
|
|
1442
|
+
f.snapshots = snapshots
|
|
1443
|
+
f.loaded = true
|
|
1444
|
+
}
|
|
1445
|
+
|
|
1446
|
+
// The serializers (default: JSON.stringify(value, null, 2)), then String()
|
|
1447
|
+
func tnSnapshotSerialize(value interface{}, options []interface{}) string {
|
|
1448
|
+
serializers := tnSnapshotSerializers
|
|
1449
|
+
if len(options) > 0 && options[0] != nil {
|
|
1450
|
+
if list := TnGet(options[0], "serializers"); list != nil {
|
|
1451
|
+
serializers = tnTestList(list)
|
|
1452
|
+
}
|
|
1453
|
+
}
|
|
1454
|
+
if len(serializers) == 0 {
|
|
1455
|
+
return TnJSONStringify(value, " ")
|
|
1456
|
+
}
|
|
1457
|
+
for _, fn := range serializers {
|
|
1458
|
+
value = TnInvoke(fn, value)
|
|
1459
|
+
}
|
|
1460
|
+
return tnTestStr(value)
|
|
1461
|
+
}
|
|
1462
|
+
|
|
1463
|
+
func (a *TnTestAssert) snapshot(value interface{}, options ...interface{}) {
|
|
1464
|
+
a.ctx.asserts++
|
|
1465
|
+
file := tnSnapshotFor(a.ctx.Get_filePath())
|
|
1466
|
+
serialized := "\\n" + tnSnapshotEscape(tnSnapshotSerialize(value, options)) + "\\n"
|
|
1467
|
+
id := file.nextID(a.ctx.Get_fullName())
|
|
1468
|
+
if tnTestFilters().updateSnapshots {
|
|
1469
|
+
file.snapshots[id] = serialized
|
|
1470
|
+
return
|
|
1471
|
+
}
|
|
1472
|
+
file.read()
|
|
1473
|
+
expected, ok := file.snapshots[id]
|
|
1474
|
+
if !ok {
|
|
1475
|
+
e := tnSnapshotState("Snapshot '"+id+"' not found in '"+file.path+".' "+tnSnapshotTip, file.path)
|
|
1476
|
+
e.props["snapshot"] = id
|
|
1477
|
+
panic(e)
|
|
1478
|
+
}
|
|
1479
|
+
TnAssertStrictEqual(serialized, expected)
|
|
1480
|
+
}
|
|
1481
|
+
|
|
1482
|
+
func (a *TnTestAssert) fileSnapshot(value interface{}, path string, options ...interface{}) {
|
|
1483
|
+
a.ctx.asserts++
|
|
1484
|
+
serialized := tnSnapshotSerialize(value, options)
|
|
1485
|
+
if tnTestFilters().updateSnapshots {
|
|
1486
|
+
os.MkdirAll(filepath.Dir(path), 0o755)
|
|
1487
|
+
if err := os.WriteFile(path, []byte(serialized), 0o644); err != nil {
|
|
1488
|
+
panic(tnSnapshotState("Cannot write snapshot file '"+path+".'", path))
|
|
1489
|
+
}
|
|
1490
|
+
return
|
|
1491
|
+
}
|
|
1492
|
+
data, err := os.ReadFile(path)
|
|
1493
|
+
if err != nil {
|
|
1494
|
+
msg := "Cannot read snapshot file '" + path + ".'"
|
|
1495
|
+
if os.IsNotExist(err) {
|
|
1496
|
+
msg += " " + tnSnapshotTip
|
|
1497
|
+
}
|
|
1498
|
+
panic(tnSnapshotState(msg, path))
|
|
1499
|
+
}
|
|
1500
|
+
TnAssertStrictEqual(serialized, string(data))
|
|
1501
|
+
}
|
|
1502
|
+
|
|
1503
|
+
// --test-update-snapshots: each snapshot file, its entries sorted by name
|
|
1504
|
+
func tnSnapshotWriteAll() {
|
|
1505
|
+
if !tnTestFilters().updateSnapshots {
|
|
1506
|
+
return
|
|
1507
|
+
}
|
|
1508
|
+
for _, f := range tnSnapshotOrder {
|
|
1509
|
+
keys := make([]string, 0, len(f.snapshots))
|
|
1510
|
+
for k := range f.snapshots {
|
|
1511
|
+
keys = append(keys, k)
|
|
1512
|
+
}
|
|
1513
|
+
sort.Strings(keys)
|
|
1514
|
+
entries := make([]string, len(keys))
|
|
1515
|
+
for i, k := range keys {
|
|
1516
|
+
entries[i] = "exports[\`" + tnSnapshotEscape(k) + "\`] = \`" + f.snapshots[k] + "\`;\\n"
|
|
1517
|
+
}
|
|
1518
|
+
os.MkdirAll(filepath.Dir(f.path), 0o755)
|
|
1519
|
+
if err := os.WriteFile(f.path, []byte(strings.Join(entries, "\\n")), 0o644); err != nil {
|
|
1520
|
+
fmt.Fprintln(os.Stderr, "Cannot write snapshot file '"+f.path+".'")
|
|
1521
|
+
TnSetExitCode(1)
|
|
1522
|
+
}
|
|
1523
|
+
}
|
|
1524
|
+
}`;
|
|
969
1525
|
function eventFn(nodes) {
|
|
970
1526
|
void nodes;
|
|
971
1527
|
return ['*TnTestContext', 'func(...interface{})'];
|
|
972
1528
|
}
|
|
973
1529
|
export function registerTest(r) {
|
|
974
1530
|
defineModule(r, 'test');
|
|
975
|
-
defineHelper(r, 'nodeTest', ['fmt', 'math', 'os', 'reflect', 'strconv', 'strings', 'time'], ['eventLoop', 'promise', 'nodeInvoke', 'dynamic', 'nodeEvents', 'nodeProcess', 'nodeUtil', 'error', 'timerWait', 'clock'], testHelper);
|
|
1531
|
+
defineHelper(r, 'nodeTest', ['fmt', 'math', 'os', 'path/filepath', 'reflect', 'regexp', 'strconv', 'strings', 'time'], ['eventLoop', 'promise', 'nodeInvoke', 'dynamic', 'nodeEvents', 'nodeProcess', 'nodeUtil', 'error', 'timerWait', 'clock', 'nodeTestReport', 'nodeTestClock@os'], testHelper);
|
|
1532
|
+
defineHelper(r, 'nodeTestSnapshot', ['fmt', 'os', 'path/filepath', 'sort', 'strconv', 'strings'], ['nodeTest', 'nodeAssert', 'jsonStringify'], snapshotHelper);
|
|
1533
|
+
registerTestReporters(r);
|
|
1534
|
+
registerTestRun(r);
|
|
1535
|
+
registerTestCoverage(r);
|
|
976
1536
|
const use = () => r.helper('nodeTest');
|
|
1537
|
+
// "file", line, column of a test() call: where Node's reports point
|
|
1538
|
+
const site = (c) => {
|
|
1539
|
+
const position = r.callSite(c.site);
|
|
1540
|
+
return `${JSON.stringify(position.file)}, ${position.line}, ${position.column}`;
|
|
1541
|
+
};
|
|
977
1542
|
defineClass(r, 'test.TestContext', '*TnTestContext', 'class', 'skip=; todo=; diagnostic=; plan=; runOnly=; test=chan struct{}; before=; after=; beforeEach=; afterEach=', '', 'name=string; fullName=string; mock=*TnMockTracker; signal=*TnAbortSignal; filePath=string; assert=*TnTestAssert', '');
|
|
1543
|
+
r.methods['test.TestContext#test'] = (c) => {
|
|
1544
|
+
use();
|
|
1545
|
+
return `${c.recv}.test(${site(c)}${c.args.length > 0 ? ', ' : ''}${c.args.join(', ')})`;
|
|
1546
|
+
};
|
|
978
1547
|
r.classes['test.SuiteContext'] = r.classes['test.TestContext'];
|
|
979
1548
|
defineClass(r, 'test.MockTracker', '*TnMockTracker', 'class', 'reset=; restoreAll=', '', 'timers=*TnMockTimers', '');
|
|
980
1549
|
defineClass(r, 'test.MockTimers', '*TnMockTimers', 'class', 'enable=; tick=; runAll=; setTime=; reset=', '', '', '');
|
|
981
1550
|
// t.assert.x(...): node:assert's x, counted for t.plan()
|
|
982
|
-
defineClass(r, 'test.TestContextAssert', '*TnTestAssert', 'class', '', '', '', '');
|
|
1551
|
+
defineClass(r, 'test.TestContextAssert', '*TnTestAssert', 'class', 'snapshot=; fileSnapshot=', '', '', '');
|
|
1552
|
+
for (const name of ['snapshot', 'fileSnapshot']) {
|
|
1553
|
+
r.methods[`test.TestContextAssert#${name}`] = (c) => {
|
|
1554
|
+
r.helper('nodeTestSnapshot');
|
|
1555
|
+
return `${c.recv}.${name}(${c.args.join(', ')})`;
|
|
1556
|
+
};
|
|
1557
|
+
}
|
|
983
1558
|
const assertNames = ['ok', 'equal', 'notEqual', 'strictEqual', 'notStrictEqual', 'deepEqual', 'notDeepEqual', 'deepStrictEqual', 'notDeepStrictEqual', 'throws', 'doesNotThrow', 'rejects', 'doesNotReject', 'match', 'doesNotMatch', 'fail', 'ifError'];
|
|
984
1559
|
for (const name of assertNames) {
|
|
985
1560
|
const key = `test.TestContextAssert#${name}`;
|
|
@@ -1002,7 +1577,7 @@ export function registerTest(r) {
|
|
|
1002
1577
|
const register = (name, goFn, variant) => {
|
|
1003
1578
|
r.functions[`test.${name}`] = (c) => {
|
|
1004
1579
|
use();
|
|
1005
|
-
return `${goFn}(${JSON.stringify(variant)}${c.args.length > 0 ? ', ' : ''}${c.args.join(', ')})`;
|
|
1580
|
+
return `${goFn}(${JSON.stringify(variant)}, ${site(c)}${c.args.length > 0 ? ', ' : ''}${c.args.join(', ')})`;
|
|
1006
1581
|
};
|
|
1007
1582
|
r.results[`test.${name}`] = 'chan struct{}';
|
|
1008
1583
|
r.paramTypers[`test.${name}`] = eventFn;
|
|
@@ -1011,13 +1586,13 @@ export function registerTest(r) {
|
|
|
1011
1586
|
register(name, 'TnTestRegister', '');
|
|
1012
1587
|
register(`${name}.skip`, 'TnTestRegister', 'skip');
|
|
1013
1588
|
register(`${name}.todo`, 'TnTestRegister', 'todo');
|
|
1014
|
-
register(`${name}.only`, 'TnTestRegister', '');
|
|
1589
|
+
register(`${name}.only`, 'TnTestRegister', 'only');
|
|
1015
1590
|
}
|
|
1016
1591
|
for (const name of ['describe', 'suite']) {
|
|
1017
1592
|
register(name, 'TnSuiteRegister', '');
|
|
1018
1593
|
register(`${name}.skip`, 'TnSuiteRegister', 'skip');
|
|
1019
1594
|
register(`${name}.todo`, 'TnSuiteRegister', 'todo');
|
|
1020
|
-
register(`${name}.only`, 'TnSuiteRegister', '');
|
|
1595
|
+
register(`${name}.only`, 'TnSuiteRegister', 'only');
|
|
1021
1596
|
}
|
|
1022
1597
|
for (const variant of ['', '.skip', '.todo', '.only']) {
|
|
1023
1598
|
r.functions[`test.default${variant}`] = r.functions[`test.test${variant}`];
|
|
@@ -1031,6 +1606,18 @@ export function registerTest(r) {
|
|
|
1031
1606
|
};
|
|
1032
1607
|
r.paramTypers[`test.${hook}`] = eventFn;
|
|
1033
1608
|
}
|
|
1609
|
+
// snapshot.setDefaultSnapshotSerializers / setResolveSnapshotPath
|
|
1610
|
+
r.namespaces['test.default.snapshot'] = 'test.snapshot';
|
|
1611
|
+
r.objects['test.snapshot'] = true;
|
|
1612
|
+
r.functions['test.snapshot.setDefaultSnapshotSerializers'] = (c) => {
|
|
1613
|
+
r.helper('nodeTestSnapshot');
|
|
1614
|
+
return `TnSetDefaultSnapshotSerializers(${c.args[0]})`;
|
|
1615
|
+
};
|
|
1616
|
+
r.functions['test.snapshot.setResolveSnapshotPath'] = (c) => {
|
|
1617
|
+
r.helper('nodeTestSnapshot');
|
|
1618
|
+
return `TnSetResolveSnapshotPath(${c.args[0]})`;
|
|
1619
|
+
};
|
|
1620
|
+
r.paramTypers['test.snapshot.setResolveSnapshotPath'] = (_n) => ['string'];
|
|
1034
1621
|
// mock.fn(original?, implementation?) / t.mock.fn(...)
|
|
1035
1622
|
r.values['test.mock'] = (_c) => {
|
|
1036
1623
|
use();
|
|
@@ -1050,6 +1637,19 @@ export function registerTest(r) {
|
|
|
1050
1637
|
return `TnMockFn(TnMock, ${c.args.join(', ')})`;
|
|
1051
1638
|
};
|
|
1052
1639
|
r.typers['test.mock.fn'] = (c) => (c.nodes.length === 0 || !c.types[0] ? 'func(...interface{}) interface{}' : c.types[0]);
|
|
1640
|
+
// mock.method(object, name, implementation?, options?) / getter / setter
|
|
1641
|
+
for (const kind of ['method', 'getter', 'setter']) {
|
|
1642
|
+
r.methods[`test.MockTracker#${kind}`] = (c) => {
|
|
1643
|
+
use();
|
|
1644
|
+
return r.mockMember(c, c.recv, kind);
|
|
1645
|
+
};
|
|
1646
|
+
r.functions[`test.mock.${kind}`] = (c) => {
|
|
1647
|
+
use();
|
|
1648
|
+
return r.mockMember(c, 'TnMock', kind);
|
|
1649
|
+
};
|
|
1650
|
+
r.typers[`test.MockTracker#${kind}`] = (c) => r.mockMemberType(c, kind);
|
|
1651
|
+
r.typers[`test.mock.${kind}`] = r.typers[`test.MockTracker#${kind}`];
|
|
1652
|
+
}
|
|
1053
1653
|
for (const method of ['reset', 'restoreAll']) {
|
|
1054
1654
|
r.functions[`test.mock.${method}`] = (_c) => {
|
|
1055
1655
|
use();
|