-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ability to read tests in ".jsx" and ".tsx" files
- Loading branch information
Showing
12 changed files
with
2,638 additions
and
373 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const TRANSFORM_EXTENSIONS = [".jsx", ".cjsx", ".mjsx", ".tsx", ".ctsx", ".mtsx"]; | ||
export const JS_EXTENSION_RE = /^\.([cm]?[tj]sx?|json)$/; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./test-transformer"; | ||
export * from "./constants"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import * as nodePath from "node:path"; | ||
import * as babel from "@babel/core"; | ||
import { addHook } from "pirates"; | ||
import { TRANSFORM_EXTENSIONS, JS_EXTENSION_RE } from "./constants"; | ||
|
||
import type { NodePath, PluginObj, TransformOptions } from "@babel/core"; | ||
import type { ImportDeclaration } from "@babel/types"; | ||
|
||
export const setupTransformHook = (): VoidFunction => { | ||
const transformOptions: TransformOptions = { | ||
browserslistConfigFile: false, | ||
babelrc: false, | ||
configFile: false, | ||
compact: false, | ||
plugins: [ | ||
[ | ||
require("@babel/plugin-transform-react-jsx"), | ||
{ | ||
throwIfNamespace: false, | ||
runtime: "automatic", | ||
}, | ||
], | ||
require("@babel/plugin-transform-modules-commonjs"), | ||
[ | ||
(): PluginObj => ({ | ||
name: "ignore-imports", | ||
visitor: { | ||
ImportDeclaration(path: NodePath<ImportDeclaration>): void { | ||
const extname = nodePath.extname(path.node.source.value); | ||
|
||
if (extname && !extname.match(JS_EXTENSION_RE)) { | ||
path.remove(); | ||
} | ||
}, | ||
}, | ||
}), | ||
], | ||
], | ||
}; | ||
|
||
const revertTransformHook = addHook( | ||
(originalCode, filename) => { | ||
return babel.transform(originalCode, { filename, ...transformOptions })!.code as string; | ||
}, | ||
{ exts: TRANSFORM_EXTENSIONS }, | ||
); | ||
|
||
return revertTransformHook; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
export const setupTransformHook: () => VoidFunction = require("../bundle").setupTransformHook; | ||
export const TRANSFORM_EXTENSIONS: string[] = require("../bundle").TRANSFORM_EXTENSIONS; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import * as pirates from "pirates"; | ||
import sinon, { SinonStub } from "sinon"; | ||
import { setupTransformHook, TRANSFORM_EXTENSIONS } from "../../../src/test-reader/test-transformer"; | ||
|
||
describe("test-transformer", () => { | ||
const sandbox = sinon.createSandbox(); | ||
|
||
beforeEach(() => { | ||
sandbox.stub(pirates, "addHook").returns(sandbox.stub()); | ||
}); | ||
|
||
afterEach(() => sandbox.restore()); | ||
|
||
describe("setupTransformHook", () => { | ||
it("should return function to revert transformation", () => { | ||
const revertFn = sandbox.stub(); | ||
(pirates.addHook as SinonStub).returns(revertFn); | ||
|
||
assert.equal(setupTransformHook(), revertFn); | ||
}); | ||
|
||
describe("should transform", () => { | ||
TRANSFORM_EXTENSIONS.forEach(extName => { | ||
it(`component with extension: "${extName}"`, () => { | ||
let transformedCode; | ||
(pirates.addHook as SinonStub).callsFake(cb => { | ||
transformedCode = cb(`import "some${extName}"`); | ||
}); | ||
|
||
setupTransformHook(); | ||
|
||
assert.exists(transformedCode, `require("some${extName}")`); | ||
}); | ||
}); | ||
|
||
it("modules without extension", () => { | ||
let transformedCode; | ||
(pirates.addHook as SinonStub).callsFake(cb => { | ||
transformedCode = cb('import "some-module"'); | ||
}); | ||
|
||
setupTransformHook(); | ||
|
||
assert.exists(transformedCode, 'require("some-module")'); | ||
}); | ||
|
||
it(".json", () => { | ||
let transformedCode; | ||
(pirates.addHook as SinonStub).callsFake(cb => { | ||
transformedCode = cb('import "some.json"'); | ||
}); | ||
|
||
setupTransformHook(); | ||
|
||
assert.exists(transformedCode, 'require("some.json")'); | ||
}); | ||
}); | ||
|
||
describe("should not transform", () => { | ||
[".css", ".less", ".scss", ".jpg", ".png", ".woff"].forEach(extName => { | ||
it(`asset with extension: "${extName}"`, () => { | ||
let transformedCode; | ||
(pirates.addHook as SinonStub).callsFake(cb => { | ||
transformedCode = cb(`import "some${extName}"`); | ||
}); | ||
|
||
setupTransformHook(); | ||
|
||
assert.equal(transformedCode, '"use strict";'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters