Skip to content
This repository has been archived by the owner on Feb 8, 2024. It is now read-only.

Commit

Permalink
Test: Re-write unit tests (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW authored Dec 19, 2021
1 parent 377bad1 commit a48dbbf
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 45 deletions.
16 changes: 9 additions & 7 deletions __tests__/browserify.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ const path = require("path");
const vm = require("vm");

describe("browserify", function() {
it("@swc/register may be used without breaking browserify", function(done) {
it("@swc/register may be used without breaking browserify", () => {
const bundler = browserify(
path.join(__dirname, "fixtures/browserify/register.js")
);

bundler.bundle(function(err, bundle) {
if (err) return done(err);
expect(bundle.length).toBeTruthy();
return new Promise((resolve, reject) => {
bundler.bundle(function (err, bundle) {
if (err) return reject(err);
expect(bundle.length).toBeTruthy();

// ensure that the code runs without throwing an exception
vm.runInNewContext("var global = this;\n" + bundle, {});
done();
// ensure that the code runs without throwing an exception
vm.runInNewContext("var global = this;\n" + bundle, {});
resolve();
});
});
});
});
2 changes: 1 addition & 1 deletion __tests__/fixtures/browserify/register.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
require("@swc/register").default({
require("../../..").default({
ignore: false
});
66 changes: 34 additions & 32 deletions __tests__/index.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,38 @@
const fs = require("fs");
const path = require("path");

let currentHook;
let currentOptions;
let sourceMapSupport = false;

const registerFile = require.resolve("../lib/node");
const testFile = require.resolve("./fixtures/swcrc/es2015");
const testFileContent = fs.readFileSync(testFile);

jest.mock("pirates", () => {
return {
addHook(hook, opts) {
currentHook = hook;
currentOptions = opts;

return () => {
currentHook = null;
currentOptions = null;
};
}
};
});

jest.mock("source-map-support", () => {
return {
install() {
sourceMapSupport = true;
}
};
});

const defaultOptions = {
exts: [".js", ".jsx", ".es6", ".es", ".mjs", ".ts", ".tsx"],
ignoreNodeModules: false
};

describe("@swc/register", function() {
let currentHook, currentOptions, sourceMapSupport;

const mocks = {
['pirates']: {
addHook(hook, opts) {
currentHook = hook;
currentOptions = opts;

return () => {
currentHook = null;
currentOptions = null;
};
},
},

['source-map-support']: {
install() {
sourceMapSupport = true;
},
},
};

let swcRegister;

function setupRegister(config = { swcrc: false }) {
Expand All @@ -57,14 +53,20 @@ describe("@swc/register", function() {
}
}

afterEach(() => {
revertRegister();
beforeEach(() => {
currentHook = null;
currentOptions = null;
sourceMapSupport = false;
jest.resetModules();
});

afterEach(() => {
revertRegister();
});

jest.doMock("pirates", () => mocks["pirates"]);
jest.doMock("source-map-support", () => mocks["source-map-support"]);

test("registers hook correctly", () => {
setupRegister();

Expand Down Expand Up @@ -94,7 +96,7 @@ describe("@swc/register", function() {
sourceMaps: true
});

currentHook("const a = 1;", testFile);
currentHook("const a = 2;", testFile);

expect(sourceMapSupport).toBe(true);
});
Expand All @@ -105,7 +107,7 @@ describe("@swc/register", function() {
sourceMaps: false
});

currentHook("const a = 1;", testFile);
currentHook("const a = 3;", testFile);

expect(sourceMapSupport).toBe(false);
});
Expand All @@ -121,7 +123,7 @@ describe("@swc/register", function() {

const result = currentHook(testFileContent, testFile);

expect(result.replace(/\n/g, "")).toBe("'use strict';require('assert');");
expect(result).toBe('"use strict";\nrequire("assert");\n');
});

test("hook transpiles with swcrc", () => {
Expand All @@ -132,6 +134,6 @@ describe("@swc/register", function() {

const result = currentHook(testFileContent, testFile);

expect(result.replace(/\n/g, "")).toBe("'use strict';require('assert');");
expect(result).toBe('"use strict";\nrequire("assert");\n');
});
});
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
"prepare": "npm run build",
"build": "tsc -d"
},
"jest": {
"testPathIgnorePatterns": ["fixtures"]
},
"repository": {
"type": "git",
"url": "git+https://github.com/swc-project/register.git"
Expand Down
8 changes: 3 additions & 5 deletions src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function mtime(filename: string) {
function compile(code: string | any, filename: string) {
// merge in base options and resolve all the plugins and presets relative to this file
const opts = {
sourceRoot: path.dirname(filename),
sourceRoot: path.dirname(filename) + path.sep,
...deepClone(transformOpts),
filename
};
Expand All @@ -61,7 +61,7 @@ function compile(code: string | any, filename: string) {
delete opts.ignore;
const output: swc.Output = swc.transformSync(code, {
...opts,
sourceMaps: opts.sourceMaps === undefined ? "inline" : opts.sourceMaps
sourceMaps: opts.sourceMaps === undefined ? true : opts.sourceMaps
});

if (output.map) {
Expand Down Expand Up @@ -89,15 +89,13 @@ function compileHook(code: string, filename: string) {

function hookExtensions(exts: readonly string[]) {
if (piratesRevert) piratesRevert();
piratesRevert = addHook(compileHook, { exts: exts as string[], ignoreNodeModules: true });
piratesRevert = addHook(compileHook, { exts: exts as string[], ignoreNodeModules: false });
}

export function revert() {
if (piratesRevert) piratesRevert();
}

register();

export default function register(opts: InputOptions = {}) {
// Clone to avoid mutating the arguments object with the 'delete's below.
opts = {
Expand Down

0 comments on commit a48dbbf

Please sign in to comment.