Skip to content

Commit

Permalink
test: add case for no runtime code in build
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Feb 11, 2022
1 parent dd494ba commit 0a7a1e7
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 12 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@types/debug": "^4.1.5",
"@types/fs-extra": "^9.0.11",
"@types/jest": "^27.4.0",
"@types/klaw": "^3.0.3",
"@types/node": "^14.14.31",
"@types/prompts": "^2.0.13",
"@types/rimraf": "^3.0.0",
Expand All @@ -52,6 +53,7 @@
"jest": "^27.4.7",
"jest-environment-node": "^27.4.6",
"jest-serializer-path": "^0.1.15",
"klaw": "^4.0.1",
"lint-staged": "^11.0.0",
"minimist": "^1.2.5",
"nodemon": "^2.0.15",
Expand Down
12 changes: 6 additions & 6 deletions packages/vite-plugin-checker/__tests__/e2e/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ export const testPath = expect.getState().testPath
export const testName = slash(testPath).match(/playground\/([\w-]+)\//)?.[1]
export const testDir = path.resolve(process.env.JEST_ROOT_DIR!, `./temp/${testName}`)

export function editFile(
filename: string,
replacer: (str: string) => string
// runInBuild: boolean = false
): void {
// if (isBuild && !runInBuild) return
export function editFile(filename: string, replacer: (str: string) => string): void {
const filePath = path.resolve(testDir, filename)
const content = fs.readFileSync(filePath, 'utf-8')
const modified = replacer(content)
fs.writeFileSync(filePath, modified)
}

export function editFileTo(filename: string, content: string): void {
const filePath = path.resolve(testDir, filename)
fs.writeFileSync(filePath, content)
}

export function expectStderrContains(str: string, expectedErrorMsg: string | string[]) {
const errorMsgArr = Array.isArray(expectedErrorMsg) ? expectedErrorMsg : [expectedErrorMsg]
errorMsgArr.forEach((msg) => {
Expand Down
43 changes: 43 additions & 0 deletions playground/basic/__tests__/no-runtime-code-in-build.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import fs from 'fs/promises'
import klaw from 'klaw'
import path from 'path'
import { preTest, viteBuild } from 'vite-plugin-checker/__tests__/e2e/Sandbox/Sandbox'
import {
editFileTo,
sleep,
testDir,
WORKER_CLEAN_TIMEOUT,
} from 'vite-plugin-checker/__tests__/e2e/testUtils'

import { copyCode } from '../../../scripts/jestSetupFilesAfterEnv'
import { serializers } from '../../../scripts/serializers'

beforeAll(async () => {
await preTest()
})

expect.addSnapshotSerializer(serializers)

afterAll(async () => {
await sleep(WORKER_CLEAN_TIMEOUT)
})

describe('no-runtime-code-in-build', () => {
beforeEach(async () => {
await copyCode()
})

it('test', async () => {
editFileTo('src/main.ts', `import { text } from './text'; console.log(text); export {}`)
await viteBuild({
cwd: testDir,
})

for await (const file of klaw(path.resolve(testDir, 'dist'))) {
if (file.stats.isFile()) {
const content = await fs.readFile(file.path, 'utf-8')
expect(content).not.toContain('vite-plugin-checker')
}
}
})
})
22 changes: 16 additions & 6 deletions pnpm-lock.yaml

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

0 comments on commit 0a7a1e7

Please sign in to comment.