-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "AwesomeGame", | ||
"version": "1.0.0", | ||
"dependencies": { | ||
"@react-native-webapis/battery-status": "*" | ||
}, | ||
"peerDependencies": { | ||
"this-should-be-ignored": "*" | ||
}, | ||
"devDependencies": { | ||
"@react-native-webapis/gamepad": "*", | ||
"invalid-polyfill-boolean": "*", | ||
"invalid-polyfill-boundary": "*", | ||
"invalid-polyfill-missing": "*" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const chalk = jest.createMockFromModule("chalk"); | ||
|
||
function passthrough(s) { | ||
return s; | ||
} | ||
|
||
chalk.cyan = passthrough; | ||
chalk.cyan.bold = passthrough; | ||
chalk.red = passthrough; | ||
chalk.red.bold = passthrough; | ||
chalk.yellow = passthrough; | ||
chalk.yellow.bold = passthrough; | ||
|
||
module.exports = chalk; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import * as os from "node:os"; | ||
import * as path from "node:path"; | ||
import { getDependencyPolyfills, resolvePath } from "../src/dependency"; | ||
|
||
describe("getDependencyPolyfills", () => { | ||
const consoleErrorSpy = jest.spyOn(global.console, "error"); | ||
|
||
beforeEach(() => { | ||
consoleErrorSpy.mockReset(); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test("collects polyfills from included valid packages", () => { | ||
const context = { | ||
projectRoot: path.join(__dirname, "__fixtures__"), | ||
}; | ||
|
||
expect(getDependencyPolyfills(context).sort()).toEqual([ | ||
expect.stringMatching( | ||
/[/\\]node_modules[/\\]@react-native-webapis[/\\]battery-status[/\\]polyfill.js$/ | ||
), | ||
expect.stringMatching( | ||
/[/\\]node_modules[/\\]@react-native-webapis[/\\]gamepad[/\\]polyfill.js$/ | ||
), | ||
]); | ||
|
||
expect(consoleErrorSpy).toBeCalledTimes(3); | ||
expect(consoleErrorSpy).toBeCalledWith( | ||
"error", | ||
expect.stringMatching(/^invalid-polyfill-boolean: invalid polyfill path/) | ||
); | ||
expect(consoleErrorSpy).toBeCalledWith( | ||
"error", | ||
expect.stringMatching(/^invalid-polyfill-boundary: invalid polyfill path/) | ||
); | ||
expect(consoleErrorSpy).toBeCalledWith( | ||
"error", | ||
expect.stringMatching(/^invalid-polyfill-missing: no such polyfill/) | ||
); | ||
}); | ||
}); | ||
|
||
describe("resolvePath", () => { | ||
test("rejects invalid paths", () => { | ||
expect(resolvePath(__dirname, "")).toBe(null); | ||
expect(resolvePath(__dirname, [])).toBe(null); | ||
expect(resolvePath(__dirname, false)).toBe(null); | ||
expect(resolvePath(__dirname, null)).toBe(null); | ||
expect(resolvePath(__dirname, undefined)).toBe(null); | ||
}); | ||
|
||
test("rejects paths outside the package boundary", () => { | ||
expect(resolvePath(__dirname, "/bin/sh")).toBe(null); | ||
|
||
expect(resolvePath(__dirname, "../../bin/sh")).toBe(null); | ||
expect(resolvePath(__dirname, "../bin/sh")).toBe(null); | ||
expect(resolvePath(__dirname, "./../bin/sh")).toBe(null); | ||
|
||
if (os.platform() === "win32") { | ||
const p = "C:\\Windows\\System32\\cmd.exe"; | ||
expect(resolvePath(__dirname, p)).toBe(null); | ||
expect(resolvePath(__dirname, p.toLowerCase())).toBe(null); | ||
|
||
expect(resolvePath(__dirname, "..\\..\\Windows\\System32\\cmd.exe")).toBe( | ||
null | ||
); | ||
expect(resolvePath(__dirname, "..\\Windows\\System32\\cmd.exe")).toBe( | ||
null | ||
); | ||
expect(resolvePath(__dirname, ".\\..\\Windows\\System32\\cmd.exe")).toBe( | ||
null | ||
); | ||
} | ||
}); | ||
|
||
test("accepts paths inside the package boundary", () => { | ||
expect(resolvePath(__dirname, "./index.js")).not.toBe(null); | ||
expect(resolvePath(__dirname, "./lib/index.js")).not.toBe(null); | ||
expect(resolvePath(__dirname, "index.js")).not.toBe(null); | ||
expect(resolvePath(__dirname, "lib/index.js")).not.toBe(null); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import * as babel from "@babel/core"; | ||
import * as path from "node:path"; | ||
|
||
describe("polyfills", () => { | ||
const currentWorkingDir = process.cwd(); | ||
const transformOptions = { | ||
plugins: [require(path.join(__dirname, "..", "lib", "index.js"))], | ||
}; | ||
|
||
function setFixture(): void { | ||
process.chdir(path.join(__dirname, "__fixtures__")); | ||
} | ||
|
||
function transform(code: string): string | null | undefined { | ||
const result = babel.transformSync(code, transformOptions); | ||
return result?.code; | ||
} | ||
|
||
afterEach(() => { | ||
process.chdir(currentWorkingDir); | ||
}); | ||
|
||
test("is noop without trigger word", () => { | ||
const code = "console.log(0);"; | ||
expect(transform(code)).toBe(code); | ||
|
||
setFixture(); | ||
expect(transform(code)).toBe(code); | ||
}); | ||
|
||
test("is noop without polyfills", () => { | ||
const code = "// @react-native-webapis\nconsole.log(0);"; | ||
expect(transform(code)).toBe(code); | ||
}); | ||
|
||
test("injects polyfills at the top", () => { | ||
setFixture(); | ||
|
||
const code = ["// @react-native-webapis", "console.log(0);"]; | ||
const result = transform(code.join("\n")); | ||
|
||
expect(result?.split("\n")).toEqual([ | ||
expect.stringMatching( | ||
/^import ".*?[/\\]{1,2}@react-native-webapis[/\\]{1,2}battery-status[/\\]{1,2}polyfill.js";$/ | ||
), | ||
expect.stringMatching( | ||
/^import ".*?[/\\]{1,2}@react-native-webapis[/\\]{1,2}gamepad[/\\]{1,2}polyfill.js";$/ | ||
), | ||
...code, | ||
]); | ||
}); | ||
}); |