1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { primordials } from "ext:core/mod.js";
const { PromisePrototypeThen } = primordials;
import { notImplemented, warnNotImplemented } from "ext:deno_node/_utils.ts";
export function run() {
notImplemented("test.run");
}
function noop() {}
class NodeTestContext {
#denoContext: Deno.TestContext;
constructor(t: Deno.TestContext) {
this.#denoContext = t;
}
get signal() {
notImplemented("test.TestContext.signal");
return null;
}
get name() {
notImplemented("test.TestContext.name");
return null;
}
diagnostic(message) {
// deno-lint-ignore no-console
console.log("DIAGNOSTIC:", message);
}
get mock() {
notImplemented("test.TestContext.mock");
return null;
}
runOnly() {
notImplemented("test.TestContext.runOnly");
return null;
}
skip() {
warnNotImplemented("test.TestContext.skip");
return null;
}
todo() {
warnNotImplemented("test.TestContext.todo");
return null;
}
test(name, options, fn) {
const prepared = prepareOptions(name, options, fn, {});
return PromisePrototypeThen(
this.#denoContext.step({
name: prepared.name,
fn: async (denoTestContext) => {
const newNodeTextContext = new NodeTestContext(denoTestContext);
await prepared.fn(newNodeTextContext);
},
ignore: prepared.options.todo || prepared.options.skip,
sanitizeExit: false,
sanitizeOps: false,
sanitizeResources: false,
}),
() => undefined,
);
}
before(_fn, _options) {
notImplemented("test.TestContext.before");
}
after(_fn, _options) {
notImplemented("test.TestContext.after");
}
beforeEach(_fn, _options) {
notImplemented("test.TestContext.beforeEach");
}
afterEach(_fn, _options) {
notImplemented("test.TestContext.afterEach");
}
}
function prepareOptions(name, options, fn, overrides) {
if (typeof name === "function") {
fn = name;
} else if (name !== null && typeof name === "object") {
fn = options;
options = name;
} else if (typeof options === "function") {
fn = options;
}
if (options === null || typeof options !== "object") {
options = {};
}
const finalOptions = { ...options, ...overrides };
// TODO(bartlomieju): these options are currently not handled
// const { concurrency, timeout, signal } = finalOptions;
if (typeof fn !== "function") {
fn = noop;
}
if (typeof name !== "string" || name === "") {
name = fn.name || "<anonymous>";
}
return { fn, options: finalOptions, name };
}
function wrapTestFn(fn, resolve) {
return async function (t) {
const nodeTestContext = new NodeTestContext(t);
try {
await fn(nodeTestContext);
} finally {
resolve();
}
};
}
function prepareDenoTest(name, options, fn, overrides) {
const prepared = prepareOptions(name, options, fn, overrides);
// TODO(iuioiua): Update once there's a primordial for `Promise.withResolvers()`.
// deno-lint-ignore prefer-primordials
const { promise, resolve } = Promise.withResolvers();
const denoTestOptions = {
name: prepared.name,
fn: wrapTestFn(prepared.fn, resolve),
only: prepared.options.only,
ignore: prepared.options.todo || prepared.options.skip,
sanitizeExit: false,
sanitizeOps: false,
sanitizeResources: false,
};
Deno.test(denoTestOptions);
return promise;
}
export function test(name, options, fn) {
return prepareDenoTest(name, options, fn, {});
}
test.skip = function skip(name, options, fn) {
return prepareDenoTest(name, options, fn, { skip: true });
};
test.todo = function todo(name, options, fn) {
return prepareDenoTest(name, options, fn, { todo: true });
};
test.only = function only(name, options, fn) {
return prepareDenoTest(name, options, fn, { only: true });
};
export function describe() {
notImplemented("test.describe");
}
export function it() {
notImplemented("test.it");
}
export function before() {
notImplemented("test.before");
}
export function after() {
notImplemented("test.after");
}
export function beforeEach() {
notImplemented("test.beforeEach");
}
export function afterEach() {
notImplemented("test.afterEach");
}
export const mock = {
fn: () => {
notImplemented("test.mock.fn");
},
getter: () => {
notImplemented("test.mock.getter");
},
method: () => {
notImplemented("test.mock.method");
},
reset: () => {
notImplemented("test.mock.reset");
},
restoreAll: () => {
notImplemented("test.mock.restoreAll");
},
setter: () => {
notImplemented("test.mock.setter");
},
timers: {
enable: () => {
notImplemented("test.mock.timers.enable");
},
reset: () => {
notImplemented("test.mock.timers.reset");
},
tick: () => {
notImplemented("test.mock.timers.tick");
},
runAll: () => {
notImplemented("test.mock.timers.runAll");
},
},
};
export default test;
|