Skip to content

Commit

Permalink
test: add new-test script
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Jan 11, 2025
1 parent 625880e commit cd63ae4
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ test/**/*.js.map
test/**/tsconfig.json
!test/**/node_modules
.next
.vscode
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"docs:dev": "next dev docs",
"docs:build": "next build docs",
"clean": "rm -rf ./dist",
"new-test": "node ./scripts/new-test.js",
"typecheck": "tsc --noEmit && tsc -p test/tsconfig.json --noEmit",
"prepare-release": "pnpm clean && pnpm build",
"publish-local": "pnpm prepare-release && pnpm test && pnpm publish",
Expand Down Expand Up @@ -108,7 +109,7 @@
"prettier": "^3.0.0",
"react": "^19.0.0",
"react-dom": "^19.0.0",
"typescript": "^5.6.2"
"typescript": "^5.7.2"
},
"lint-staged": {
"*.{js,jsx,ts,tsx,md,json,yml,yaml}": "prettier --write"
Expand All @@ -127,7 +128,8 @@
},
"testPathIgnorePatterns": [
"/node_modules/",
"<rootDir>/test/integration/.*/*src"
"<rootDir>/test/integration/.*/*src",
"<rootDir>/test/fixtures"
],
"testTimeout": 60000
},
Expand Down
22 changes: 11 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 68 additions & 0 deletions scripts/new-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// @ts-check

const fs = require('fs')
const path = require('path')

// Helper to copy and rename a file
const copyAndRenameFile = (srcPath, destPath, testName) => {
const content = fs.readFileSync(srcPath, 'utf8')
const updatedContent = content.replace(/<name>/g, testName) // Replace placeholder
fs.writeFileSync(destPath, updatedContent, 'utf8')
console.log(`Created file: ${destPath}`)
}

// Helper to copy folder contents recursively
const copyFolder = (srcFolder, destFolder, testName) => {
if (!fs.existsSync(destFolder)) {
fs.mkdirSync(destFolder, { recursive: true })
}

const items = fs.readdirSync(srcFolder, { withFileTypes: true })
for (const item of items) {
const srcPath = path.join(srcFolder, item.name)
const destPath = path.join(
destFolder,
item.name.replace(/test\.js/, `${testName}.test.js`),
)

if (item.isDirectory()) {
copyFolder(srcPath, destPath, testName)
} else {
copyAndRenameFile(srcPath, destPath, testName)
}
}
}

const createProjectFromFixtures = (testName) => {
const fixturesDir = path.resolve(
__dirname,
'..',
'test',
'fixtures',
'integration-test-template',
)
const testIntegrationFolder = path.resolve(
__dirname,
'..',
'test',
'integration',
)
const outputDir = path.join(testIntegrationFolder, testName)

if (!fs.existsSync(fixturesDir)) {
console.error('Fixtures folder not found!')
process.exit(1)
}

copyFolder(fixturesDir, outputDir, testName)
console.log(`Project structure created for: ${testName}`)
}

// Run the script
const testName = process.argv[2]
if (!testName) {
console.error('Please provide a test name. pnpm new-test <test-name>')
process.exit(1)
}

createProjectFromFixtures(testName)
2 changes: 1 addition & 1 deletion test/unit/memoize.test.ts → src/lib/memoize.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { memoizeByKey } from '../../src/lib/memoize'
import { memoizeByKey } from './memoize'

describe('memoize', () => {
it('should memoize the function by default based on arg', () => {
Expand Down
14 changes: 14 additions & 0 deletions test/fixtures/integration-test-template/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { createIntegrationTest, assertContainFiles } from 'testing-utils'

describe('integration - <name>', () => {
it('should work', async () => {
await createIntegrationTest(
{
directory: __dirname,
},
({ distDir }) => {
assertContainFiles(distDir, ['index.js'])
},
)
})
})
8 changes: 8 additions & 0 deletions test/fixtures/integration-test-template/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "<name>",
"main": "./dist/index.js",
"type": "module",
"exports": {
".": "./dist/index.js"
}
}
3 changes: 3 additions & 0 deletions test/fixtures/integration-test-template/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function Foo() {
return <p>Foo</p>
}
12 changes: 10 additions & 2 deletions test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@
"compilerOptions": {
"module": "ESNext",
"target": "ESNext",
"strict": false,
"baseUrl": "..",
"paths": {
"bunchee": ["./src/index.ts"]
"bunchee": ["./src/index.ts"],
"testing-utils": ["./test/integration/utils.ts"]
}
},
"include": ["./**/*.test.ts", "../src/**/*.test.ts"]
"include": [
"**/*.test.ts",
"**/*.test.tsx",
"../src/**/*.test.ts",
"../src/**/*.test.tsx"
],
"references": [{ "path": "../tsconfig.json" }]
}
6 changes: 4 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"include": ["./src", "./*.ts", "./*.d.ts", "test/unit/memoize.test.ts"],
"compilerOptions": {
"composite": true,
"rootDir": ".",
"outDir": "./dist",
"target": "ES2019",
Expand All @@ -15,5 +15,7 @@
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
},
"references": [{ "path": "./test/tsconfig.json" }],
"include": ["src", "*.d.ts"]
}

0 comments on commit cd63ae4

Please sign in to comment.