-
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
8 changed files
with
727 additions
and
414 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
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,47 @@ | ||
import * as babel from "@babel/core"; | ||
Check failure on line 1 in src/test-reader/test-transformer.ts GitHub Actions / build (18.x)
|
||
import { addHook } from "pirates"; | ||
|
||
import type { NodePath, PluginObj, TransformOptions } from "@babel/core"; | ||
Check failure on line 4 in src/test-reader/test-transformer.ts GitHub Actions / build (18.x)
|
||
import type { ImportDeclaration } from "@babel/types"; | ||
|
||
const TRANSFORM_EXTENSIONS = [".jsx", ".tsx"]; | ||
|
||
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: "remove-css-imports", | ||
visitor: { | ||
ImportDeclaration(path: NodePath<ImportDeclaration>): void { | ||
if (path.node.source.value.match(/\.(css|less|scss)$/)) { | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import * as pirates from "pirates"; | ||
import proxyquire from "proxyquire"; | ||
import sinon, { SinonStub } from "sinon"; | ||
|
||
type SetupTransformHook = typeof import("../../../src/test-reader/test-transformer").setupTransformHook; | ||
|
||
describe("test-transformer", () => { | ||
const sandbox = sinon.createSandbox(); | ||
let babelTransform: SinonStub; | ||
let setupTransformHook: SetupTransformHook; | ||
|
||
beforeEach(() => { | ||
babelTransform = sandbox.stub().returns({ code: "default-code" }); | ||
|
||
sandbox.stub(pirates, "addHook").returns(sandbox.stub()); | ||
|
||
({ setupTransformHook } = proxyquire("src/test-reader/test-transformer", { | ||
"@babel/core": { transform: babelTransform }, | ||
})).default; | ||
}); | ||
|
||
afterEach(() => sandbox.restore()); | ||
|
||
describe("setupTransformHook", () => { | ||
it("should transform '.jsx' and '.tsx' files", () => { | ||
setupTransformHook(); | ||
|
||
assert.calledOnceWith(pirates.addHook, sinon.match.any, { exts: [".jsx", ".tsx"] }); | ||
}); | ||
|
||
it("should return function to revert transformation", () => { | ||
const revertFn = sandbox.stub(); | ||
(pirates.addHook as SinonStub).returns(revertFn); | ||
|
||
assert.equal(setupTransformHook(), revertFn); | ||
}); | ||
|
||
it("should transform code using babel", () => { | ||
let transformedCode; | ||
babelTransform.returns({ code: "transformed-code" }); | ||
(pirates.addHook as SinonStub).callsFake(cb => { | ||
transformedCode = cb("original-code", "/path/test.jsx"); | ||
}); | ||
|
||
setupTransformHook(); | ||
|
||
assert.calledOnceWith(babelTransform, "original-code", { | ||
browserslistConfigFile: false, | ||
babelrc: false, | ||
configFile: false, | ||
compact: false, | ||
plugins: sinon.match.array, | ||
filename: "/path/test.jsx", | ||
}); | ||
assert.equal(transformedCode, "transformed-code"); | ||
}); | ||
}); | ||
}); |