Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial minimal set of node:assert tests #396

Merged
merged 1 commit into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/node/assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
// https://opensource.org/licenses/Apache-2.0

export * from 'node-internal:internal_assert';
import { default as assert } from 'node-internal:internal_assert';
export default assert;
2 changes: 2 additions & 0 deletions src/node/internal/internal_assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,8 @@ function isValidThenable(maybeThennable: any): boolean {
return isThenable && typeof maybeThennable !== "function";
}

export { AssertionError };

Object.assign(strict, {
AssertionError,
deepEqual: deepStrictEqual,
Expand Down
138 changes: 138 additions & 0 deletions src/workerd/api/node/assert-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if there's any value in writing these in TS?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe? I think there's a more general question there about whether workerd should handle typescript transparently if tsc is available.

deepEqual,
deepStrictEqual,
doesNotMatch,
doesNotReject,
doesNotThrow,
equal,
fail,
ifError,
match,
notDeepEqual,
notDeepStrictEqual,
notEqual,
notStrictEqual,
ok,
rejects,
strictEqual,
throws,
} from 'node:assert';

import { default as assert } from 'node:assert';

const strictEqualMessageStart = 'Expected values to be strictly equal:\n';
const start = 'Expected values to be strictly deep-equal:';
const actExp = '+ actual - expected';

function thrower(errorConstructor) {
throw new errorConstructor({});
}

export const test_ok = {
test(ctrl, env, ctx) {
// truthy values just work
[true, 1, [], {}, 'hello'].forEach(ok);
[true, 1, [], {}, 'hello'].forEach(assert);
[true, 1, [], {}, 'hello'].forEach((val) => {
doesNotThrow(() => ok(val));
});

// falsy values throw
[false, 0, '', undefined, NaN, null].forEach((val) => {
throws(() => ok(val), { name: 'AssertionError' });
});

// falsy values throw
[false, 0, '', undefined, NaN, null].forEach((val) => {
throws(() => ok(val, 'message'), { message: 'message' });
});
}
};

export const test_equal = {
test(ctrl, env, ctx) {
[
{a: 1, b: 1},
{a: 1, b: '1', fails: true},
{a: 1, b: '1', fails: true, message: 'boom'},
{a: 'a', b: 'a'},
{a: ctx, b: ctx},
].forEach(({a,b,fails,message}) => {
if (!fails) {
equal(a,b);
strictEqual(a,b);
if (message) {
throws(() => notEqual(a, b, message), { message });
throws(() => notStrictEqual(a, b, message), { message });
} else {
throws(() => notEqual(a,b), { name: 'AssertionError' });
throws(() => notStrictEqual(a,b), { name: 'AssertionError' });
}
} else {
notEqual(a, b);
notStrictEqual(a, b);
if (message) {
throws(() => equal(a,b,message), { message });
throws(() => strictEqual(a,b,message), { message });
} else {
throws(() => equal(a,b), { name: 'AssertionError' });
throws(() => strictEqual(a,b), { name: 'AssertionError' });
}
}
});
}
};

export const test_fail = {
test(ctrl, env, ctx) {
throws(() => fail("boom"), { message: "boom" });
throws(() => ifError("boom"));
throws(() => ifError(false));
doesNotThrow(() => ifError(null));
doesNotThrow(() => ifError(undefined));
}
};

export const test_rejects = {
async test(ctrl, env, ctx) {
await rejects(Promise.reject(new Error('boom')), { message: 'boom' });
await doesNotReject(Promise.resolve(1));
}
};

export const test_matching = {
test(ctrl, env, ctx) {
match('hello', /hello/);
throws(() => match('hello', /not/), { name: 'AssertionError' });
doesNotMatch('hello', /not/);
throws(() => doesNotMatch('hello', /hello/), { name: 'AssertionError' });
}
};

export const test_deep_equal = {
test(ctrl, env, ctx) {
const a = {
b: [
{
c: new Uint8Array([1,2,3]),
d: false,
e: 'hello'
}
],
};
const b = {
b: [
{
c: new Uint8Array([1,2,3]),
d: false,
e: 'hello'
}
],
};
deepEqual(a,b);
deepStrictEqual(a,b);
b.b[0].c[0] = 4;
notDeepEqual(a,b);
notDeepStrictEqual(a,b);
}
};
15 changes: 15 additions & 0 deletions src/workerd/api/node/assert-test.wd-test
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Workerd = import "/workerd/workerd.capnp";

const unitTests :Workerd.Config = (
services = [
( name = "assert-nodejs-test",
worker = (
modules = [
(name = "worker", esModule = embed "assert-test.js")
],
compatibilityDate = "2023-01-15",
compatibilityFlags = ["nodejs_compat", "experimental"]
)
),
],
);