From f2896e472e8da2330ca49864cf8535c61fe70cc3 Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Tue, 4 May 2021 14:37:05 -0400 Subject: [PATCH] chore: rewrite tests --- test/__snapshots__/index.spec.ts.snap | 82 ++++++++++ test/index.spec.ts | 222 +++----------------------- 2 files changed, 104 insertions(+), 200 deletions(-) create mode 100644 test/__snapshots__/index.spec.ts.snap diff --git a/test/__snapshots__/index.spec.ts.snap b/test/__snapshots__/index.spec.ts.snap new file mode 100644 index 0000000..4df5bcf --- /dev/null +++ b/test/__snapshots__/index.spec.ts.snap @@ -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, +} +`; diff --git a/test/index.spec.ts b/test/index.spec.ts index 091bbb4..4f4282d 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -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
hello there!!!
+ export default class Foo { + render() { + return
hello there!!!
+ } } - } - `; - - 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(); - 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() +})