-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3b55dc0
commit 5abfacf
Showing
9 changed files
with
166 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
name: Unit test & integration test | ||
|
||
on: | ||
pull_request: | ||
|
||
jobs: | ||
publish: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-node@v3 | ||
with: | ||
node-version: "16.x" | ||
|
||
- name: Run unit test | ||
run: bash tool/run_ut.sh |
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,107 @@ | ||
import fs from "fs"; | ||
|
||
import path from "path"; | ||
|
||
import os from 'os'; | ||
|
||
import { requireModule, resolveModulePath, resolvePath } from "../../src/path_resolver"; | ||
|
||
|
||
|
||
describe("path_resolver", () => { | ||
const originalEnv = process.env; | ||
|
||
let tmpDir: string = ""; | ||
let tmpPackageJsonPath: string = ""; | ||
let tmpNodeModulesDir: string = ""; | ||
|
||
beforeEach(() => { | ||
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'terra-ut-')); | ||
tmpPackageJsonPath = path.join(tmpDir, "package.json"); | ||
|
||
tmpNodeModulesDir = path.join(tmpDir, "node_modules"); | ||
|
||
fs.mkdirSync(tmpNodeModulesDir); | ||
|
||
jest.resetModules(); | ||
process.env = { | ||
...originalEnv, | ||
npm_package_json: tmpPackageJsonPath, | ||
}; | ||
}); | ||
|
||
afterEach(() => { | ||
fs.rmSync(tmpDir, { recursive: true, force: true }); | ||
process.env = originalEnv; | ||
}); | ||
|
||
describe("resolvePath", () => { | ||
it("can resolve absolute path", () => { | ||
expect(resolvePath("/a/b/c")).toEqual("/a/b/c"); | ||
}); | ||
|
||
it("can resolve absolute path with prefix", () => { | ||
expect(resolvePath("/a/b/c", "/d/e/f")).toEqual("/a/b/c"); | ||
}); | ||
|
||
it("can resolve relative path", () => { | ||
expect(resolvePath("a/b/c")).toEqual(path.resolve("a/b/c")); | ||
}); | ||
|
||
it("can resolve relative path with prefix", () => { | ||
expect(resolvePath("a/b/c", "/d/e/f")).toEqual("/d/e/f/a/b/c"); | ||
}); | ||
|
||
it("can resolve path with package schema", () => { | ||
fs.mkdirSync(path.join(tmpNodeModulesDir, "aaa")); | ||
|
||
let res = resolvePath("aaa:index.ts"); | ||
|
||
let expectRes = path.join(tmpNodeModulesDir, "aaa", "index.ts"); | ||
|
||
expect(res).toEqual(expectRes); | ||
}); | ||
}); | ||
|
||
describe("resolveModulePath", () => { | ||
it("can resolve module path", () => { | ||
fs.mkdirSync(path.join(tmpNodeModulesDir, "aaa")); | ||
|
||
let res = resolveModulePath("aaa"); | ||
|
||
let expectRes = path.join(tmpNodeModulesDir, "aaa"); | ||
|
||
expect(res).toEqual(expectRes); | ||
}); | ||
}); | ||
|
||
describe("requireModule", () => { | ||
it("can require module", () => { | ||
let testPackageDir = path.join(tmpNodeModulesDir, "aaa"); | ||
fs.mkdirSync(testPackageDir); | ||
|
||
let testPackageJsonPath = path.join(testPackageDir, "package.json"); | ||
let testPackageJsonContent = ` | ||
{ | ||
"version": "0.1.2", | ||
"main": "index.js" | ||
} | ||
`; | ||
fs.writeFileSync(testPackageJsonPath, testPackageJsonContent); | ||
|
||
let testPackageIndexJsPath = path.join(testPackageDir, "index.js"); | ||
let testPackageIndexJsContent = ` | ||
module.exports = "aaa"; | ||
`; | ||
|
||
fs.writeFileSync(testPackageIndexJsPath, testPackageIndexJsContent); | ||
|
||
|
||
let res = requireModule("aaa"); | ||
|
||
expect(res).toEqual("aaa"); | ||
}); | ||
}); | ||
}); | ||
|
||
|
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,6 @@ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
testPathIgnorePatterns: ['node_modules/', '.dist/'], | ||
testRegex: '.*\\.test\\.ts$', | ||
}; |
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
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,14 @@ | ||
#!/usr/bin/env bash | ||
set -e | ||
set -x | ||
|
||
# Find all the `__tests__`` directories to ensure there're test cases there | ||
for d in **/*__tests__*; do | ||
PACKAGE_DIR=$(realpath ${d}/..) | ||
echo "Running tests in ${PACKAGE_DIR}"; | ||
pushd $PACKAGE_DIR | ||
npm install | ||
npm run test | ||
popd | ||
done | ||
|