Skip to content

Commit

Permalink
chore: rewrite tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed May 4, 2021
1 parent 66741d5 commit f2896e4
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 200 deletions.
82 changes: 82 additions & 0 deletions test/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ts file 1`] = `
"var __create = Object.create;
var __defProp = Object.defineProperty;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __markAsModule = (target) => __defProp(target, \\"__esModule\\", {value: true});
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, {get: all[name], enumerable: true});
};
var __exportStar = (target, module2, desc) => {
if (module2 && typeof module2 === \\"object\\" || typeof module2 === \\"function\\") {
for (let key of __getOwnPropNames(module2))
if (!__hasOwnProp.call(target, key) && key !== \\"default\\")
__defProp(target, key, {get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable});
}
return target;
};
var __toModule = (module2) => {
return __exportStar(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, \\"default\\", module2 && module2.__esModule && \\"default\\" in module2 ? {get: () => module2.default, enumerable: true} : {value: module2, enumerable: true})), module2);
};
__markAsModule(exports);
__export(exports, {
display: () => display
});
var import_names = __toModule(require(\\"./names\\"));
function display() {
return import_names.default;
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4vaW5kZXgudHMiXSwic291cmNlc0NvbnRlbnQiOm51bGwsIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7O0FBQUE7QUFBQTtBQUFBO0FBQUE7QUFDSSxtQkFBa0I7QUFFWCxtQkFBbUI7QUFDeEIsU0FBTztBQUFBOyIsIm5hbWVzIjpbXX0="
`;

exports[`ts file 2`] = `
Object {
"mappings": ";;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AACI,mBAAkB;AAEX,mBAAmB;AACxB,SAAO;AAAA;",
"names": Array [],
"sources": Array [
"./index.ts",
],
"sourcesContent": null,
"version": 3,
}
`;

exports[`tsx file 1`] = `
"var __defProp = Object.defineProperty;
var __markAsModule = (target) => __defProp(target, \\"__esModule\\", {value: true});
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, {get: all[name], enumerable: true});
};
__markAsModule(exports);
__export(exports, {
default: () => Foo
});
class Foo {
render() {
return /* @__PURE__ */ React.createElement(\\"div\\", {
className: \\"hehe\\"
}, \\"hello there!!!\\");
}
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4vaW5kZXgudHN4Il0sInNvdXJjZXNDb250ZW50IjpudWxsLCJtYXBwaW5ncyI6Ijs7Ozs7O0FBQUE7QUFBQTtBQUFBO0FBQUE7QUFDSSxVQUF5QjtBQUFBLEVBQ3ZCLFNBQVM7QUFDUCxXQUFPLG9DQUFDLE9BQUQ7QUFBQSxNQUFLLFdBQVU7QUFBQSxPQUFPO0FBQUE7QUFBQTsiLCJuYW1lcyI6W119"
`;

exports[`tsx file 2`] = `
Object {
"mappings": ";;;;;;AAAA;AAAA;AAAA;AAAA;AACI,UAAyB;AAAA,EACvB,SAAS;AACP,WAAO,oCAAC,OAAD;AAAA,MAAK,WAAU;AAAA,OAAO;AAAA;AAAA;",
"names": Array [],
"sources": Array [
"./index.tsx",
],
"sourcesContent": null,
"version": 3,
}
`;
222 changes: 22 additions & 200 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,212 +1,34 @@
import mockfs from "mock-fs";
import { process } from "../src/index";
import mockfs = require('mock-fs')
import esbuildJest from '../src/index'

afterEach(() => {
mockfs.restore();
});

test("ts file", () => {
const content = `
import names from './names'
export function display() {
return names
}
`;

mockfs({
"./tests/index.spec.ts": content,
});

const output = process(content, "./tests/index.spec.ts", {
transform: [
[
"^.+\\.ts?$",
"./dist/esbuild-jest.js"
]
],
});

expect(output.code).toMatchInlineSnapshot(`
"var __create = Object.create;
var __defProp = Object.defineProperty;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __markAsModule = (target) => __defProp(target, \\"__esModule\\", {value: true});
var __export = (target, all) => {
__markAsModule(target);
for (var name in all)
__defProp(target, name, {get: all[name], enumerable: true});
};
var __exportStar = (target, module2, desc) => {
__markAsModule(target);
if (module2 && typeof module2 === \\"object\\" || typeof module2 === \\"function\\") {
for (let key of __getOwnPropNames(module2))
if (!__hasOwnProp.call(target, key) && key !== \\"default\\")
__defProp(target, key, {get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable});
}
return target;
};
var __toModule = (module2) => {
if (module2 && module2.__esModule)
return module2;
return __exportStar(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, \\"default\\", {value: module2, enumerable: true}), module2);
};
__export(exports, {
display: () => display
});
var import_names = __toModule(require(\\"./names\\"));
function display() {
return import_names.default;
}
"
`);
});

test("with transformOptions", () => {
const content = `
import names from './names'
export function display() {
return names
}
`;
const { process } = esbuildJest.createTransformer({})

mockfs({
"./tests/index.spec.ts": content,
});

const output = process(content, "./tests/index.spec.ts", {
transform: [
[
"^.+\\.ts?$",
"./dist/esbuild-jest.js",
{
format: "esm",
sourcemap: false
},
],
],
});

expect(output.code).toMatchInlineSnapshot(`
"import names from \\"./names\\";
function display() {
return names;
}
export {
display
};
"
`);

expect(output.map).toBeNull();
});
afterEach(() => {
mockfs.restore()
})

test("with sourcemaps", () => {
test('ts file', () => {
const content = `
import names from './names'
export function display() {
return names
}
`;

mockfs({
"./tests/index.spec.ts": content,
});

const output = process(content, "./tests/index.spec.ts", {
transform: [
[
"^.+\\.ts?$",
"./dist/esbuild-jest.js",
{
format: "esm",
sourcemap: true
},
],
],
});

expect(output.code).toMatchInlineSnapshot(`
"import names from \\"./names\\";
function display() {
return names;
}
export {
display
};
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi90ZXN0cy9pbmRleC5zcGVjLnRzIl0sCiAgInNvdXJjZXNDb250ZW50IjogWyJcbiAgICBpbXBvcnQgbmFtZXMgZnJvbSAnLi9uYW1lcydcblxuICAgIGV4cG9ydCBmdW5jdGlvbiBkaXNwbGF5KCkge1xuICAgICAgcmV0dXJuIG5hbWVzXG4gICAgfVxuICAiXSwKICAibWFwcGluZ3MiOiAiQUFDSTtBQUVPO0FBQ0wsU0FBTztBQUFBOyIsCiAgIm5hbWVzIjogW10KfQo=
"
`);

expect(output.map).toEqual({"version":3,"sources":["./tests/index.spec.ts"],"sourcesContent":null,"mappings":"AACI;AAEO;AACL,SAAO;AAAA;","names":[]});
});
`
const output = process(content, './index.ts')
expect(output.code).toMatchSnapshot()
expect(output.map).toMatchSnapshot()
})

test("load index.(x)", async () => {
test('tsx file', async () => {
const content = `
export default class Foo {
render() {
return <div className="hehe">hello there!!!</div>
export default class Foo {
render() {
return <div className="hehe">hello there!!!</div>
}
}
}
`;

const tests = `
import React from 'react';
import ReactTestUtils from 'react-addons-test-utils';
import Foo from '../src/index';
const Renderer = ReactTestUtils.createRenderer();
describe('Example1', () => {
it('should render correctly', () => {
Renderer.render(<Foo />);
const result = Renderer.getRenderOutput();
expect(result.type).toBe('div');
});
});
`;

mockfs({
"./src/index.tsx": content,
"./tests/index.spec.ts": tests,
});

const output = process(tests, "./tests/index.spec.ts", {
transform: [
[
"^.+\\.ts?$",
"./dist/esbuild-jest.js",
{
format: "esm",
sourcemap: false,
loaders: {
'.spec.ts': 'tsx'
}
},
],
],
});

expect(output.code).toMatchInlineSnapshot(`
"import React from \\"react\\";
import ReactTestUtils from \\"react-addons-test-utils\\";
import Foo from \\"../src/index\\";
const Renderer = ReactTestUtils.createRenderer();
describe(\\"Example1\\", () => {
it(\\"should render correctly\\", () => {
Renderer.render(/* @__PURE__ */ React.createElement(Foo, null));
const result = Renderer.getRenderOutput();
expect(result.type).toBe(\\"div\\");
});
});
"
`)

expect(output.map).toBeNull();
});
`
const output = process(content, './index.tsx')
expect(output.code).toMatchSnapshot()
expect(output.map).toMatchSnapshot()
})

0 comments on commit f2896e4

Please sign in to comment.