Skip to content

Commit

Permalink
test(napi/transform): use the expect API instead of assert (#7739)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Dec 9, 2024
1 parent c6a19aa commit c254a5b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 28 deletions.
4 changes: 2 additions & 2 deletions napi/transform/test/id.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert, describe, it } from 'vitest';
import { describe, expect, it } from 'vitest';

import oxc from '../index';

Expand All @@ -17,7 +17,7 @@ describe('isolated declaration', () => {

it('matches output', () => {
const ret = oxc.isolatedDeclaration('test.ts', code, { sourcemap: true });
assert.deepEqual(ret, {
expect(ret).toStrictEqual({
code: '/**\n' +
'* jsdoc 1\n' +
'*/\n' +
Expand Down
60 changes: 34 additions & 26 deletions napi/transform/test/transform.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert, describe, it, test } from 'vitest';
import { describe, expect, it, test } from 'vitest';

import { HelperMode, transform } from '../index';

Expand All @@ -7,7 +7,7 @@ describe('simple', () => {

it('matches output', () => {
const ret = transform('test.ts', code, { sourcemap: true });
assert.deepEqual(ret, {
expect(ret).toStrictEqual({
code: 'export class A {}\n',
errors: [],
map: {
Expand All @@ -22,17 +22,29 @@ describe('simple', () => {

it('uses the `lang` option', () => {
const ret = transform('test.vue', code, { lang: 'ts' });
assert.equal(ret.code, 'export class A {}\n');
expect(ret.code).toEqual('export class A {}\n');
});

it('uses the `declaration` option', () => {
const ret = transform('test.ts', code, { typescript: { declaration: {} } });
assert.equal(ret.declaration, 'export declare class A<T> {}\n');
expect(ret.declaration).toEqual('export declare class A<T> {}\n');
});

it('uses the `sourcemap` option', () => {
const ret = transform('test.ts', code, { typescript: { declaration: {} }, sourcemap: true });
assert(ret.declarationMap);
expect(ret.declarationMap).toStrictEqual(
{
'mappings': 'AAAA,OAAO,cAAM,EAAE,GAAG,CAAE',
'names': [],
'sources': [
'test.ts',
],
'sourcesContent': [
'export class A<T> {}',
],
'version': 3,
},
);
});
});

Expand All @@ -50,7 +62,7 @@ describe('transform', () => {
];
for (const code of cases) {
const ret = transform('test.ts', code);
assert.equal(ret.code.trim(), code);
expect(ret.code.trim()).toEqual(code);
}
});
});
Expand All @@ -71,24 +83,24 @@ describe('target', () => {
test.each(data)('transform %s', (target, code) => {
// Also test array syntax.
const ret = transform('test.js', code, { target: [target] });
assert(ret.errors.length == 0);
assert(ret.code);
assert.notEqual(ret.code, code);
expect(ret.errors.length).toBe(0);
expect(ret.code).toBeDefined();
expect(ret.code).not.toEqual(code);
});

test.each(data)('no transform esnext: %s', (_target, code) => {
const ret = transform('test.js', code, { target: 'esnext' });
assert(ret.errors.length == 0);
assert(ret.code);
assert.equal(ret.code, code);
expect(ret.errors.length).toBe(0);
expect(ret.code).toBeDefined();
expect(ret.code).toEqual(code);
});

it('should turn off class propertiers because plugin is not ready', () => {
const code = 'class Foo {\n\t#a;\n}\n';
const ret = transform('test.js', code, { target: 'es2015' });
assert(ret.errors.length == 0);
assert(ret.code);
assert.equal(ret.code, code);
expect(ret.errors.length).toBe(0);
expect(ret.code).toBeDefined();
expect(ret.code).toEqual(code);
});
});

Expand All @@ -104,7 +116,7 @@ describe('helpers', () => {
target: 'es2015',
helpers: { mode },
});
assert.equal(ret.code, expected);
expect(ret.code).toEqual(expected);
});
});

Expand All @@ -119,11 +131,8 @@ import bar = require('bar')
declaration: {},
},
});
assert.deepEqual(ret, {
code: 'module.exports = function foo() {};\nconst bar = require("bar");\n',
declaration: 'declare const _default: () => void;\nexport = _default;\n',
errors: [],
});
expect(ret.code).toEqual('module.exports = function foo() {};\nconst bar = require("bar");\n');
expect(ret.declaration).toEqual('declare const _default: () => void;\nexport = _default;\n');
});
});

Expand All @@ -136,8 +145,7 @@ describe('react refresh plugin', () => {

it('matches output', () => {
const ret = transform('test.tsx', code, { jsx: { refresh: {} } });
assert.equal(
ret.code,
expect(ret.code).toEqual(
`import { useState } from "react";
import { jsxs as _jsxs } from "react/jsx-runtime";
var _s = $RefreshSig$();
Expand Down Expand Up @@ -166,7 +174,7 @@ describe('define plugin', () => {
'process.env.NODE_ENV': '"development"',
},
});
assert.equal(ret.code, '');
expect(ret.code).toEqual('');
});

it('handles typescript declare global', () => {
Expand All @@ -176,7 +184,7 @@ describe('define plugin', () => {
'__TEST_DEFINE__': '"replaced"',
},
});
assert.equal(ret.code, 'console.log({ __TEST_DEFINE__: "replaced" });\n');
expect(ret.code).toEqual('console.log({ __TEST_DEFINE__: "replaced" });\n');
});
});

Expand All @@ -189,6 +197,6 @@ describe('inject plugin', () => {
'Object.assign': 'foo',
},
});
assert.equal(ret.code, 'import $inject_Object_assign from "foo";\nlet _ = $inject_Object_assign;\n');
expect(ret.code).toEqual('import $inject_Object_assign from "foo";\nlet _ = $inject_Object_assign;\n');
});
});

0 comments on commit c254a5b

Please sign in to comment.