Skip to content

Commit

Permalink
test: extract & simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Nov 21, 2024
1 parent fa48715 commit d5fb870
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 125 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
"@swc/core": "^1.9.2",
"@sxzz/eslint-config": "^4.4.1",
"@sxzz/prettier-config": "^2.0.2",
"@sxzz/test-utils": "^0.3.7",
"@types/debug": "^4.1.12",
"@types/node": "^22.9.0",
"bumpp": "^9.8.1",
Expand Down
29 changes: 29 additions & 0 deletions pnpm-lock.yaml

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

24 changes: 11 additions & 13 deletions tests/__snapshots__/esbuild.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`esbuild > generate mode 1`] = `
[
"// <stdout>
"// <stdout>
// tests/fixtures/basic/component.tsx
function Component() {
return /* @__PURE__ */ React.createElement("div", null, "I'm a div in a tsx component!");
Expand All @@ -19,25 +18,24 @@ export {
hello,
num
};
",
"// temp/main.d.ts
// temp/component.d.ts
export declare function Component(): React.JSX.Element;
// temp/main.d.ts
import { type Num } from './types';
export type Str = string;
export declare function hello(s: Str): Str;
export declare let c: React.JSX.Element;
export declare let num: Num;
",
"// temp/types.d.ts
// temp/types.d.ts
import type { Num2 } from './types2';
export type Num = Num2;
",
"// temp/types2.d.ts
// temp/types2.d.ts
export type Num2 = number;
",
"// temp/component.d.ts
export declare function Component(): React.JSX.Element;
",
]
"
`;
exports[`esbuild > write mode 1`] = `
Expand Down
22 changes: 10 additions & 12 deletions tests/__snapshots__/rolldown.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`rolldown 1`] = `
[
"// main.js
"// main.js
import { jsx as _jsx } from "react/jsx-runtime";
//#region tests/fixtures/basic/component.tsx
Expand All @@ -19,23 +18,22 @@ let c = Component;
let num = 1;
//#endregion
export { c, hello, num };",
"// temp/component.d.ts
export { c, hello, num };
// temp/component.d.ts
export declare function Component(): React.JSX.Element;
",
"// temp/main.d.ts
// temp/main.d.ts
import { type Num } from "./types";
export type Str = string;
export declare function hello(s: Str): Str;
export declare let c: React.JSX.Element;
export declare let num: Num;
",
"// temp/types.d.ts
// temp/types.d.ts
import type { Num2 } from "./types2";
export type Num = Num2;
",
"// temp/types2.d.ts
// temp/types2.d.ts
export type Num2 = number;
",
]
"
`;
24 changes: 11 additions & 13 deletions tests/__snapshots__/rollup.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`rollup > generate basic 1`] = `
[
"// main.js
"// main.js
function Component() {
return /* @__PURE__ */ React.createElement("div", null, "I'm a div in a tsx component!");
}
Expand All @@ -14,25 +13,24 @@ let c = Component;
let num = 1;
export { c, hello, num };
",
"// temp/main.d.ts
// temp/component.d.ts
export declare function Component(): React.JSX.Element;
// temp/main.d.ts
import { type Num } from './types';
export type Str = string;
export declare function hello(s: Str): Str;
export declare let c: React.JSX.Element;
export declare let num: Num;
",
"// temp/types.d.ts
// temp/types.d.ts
import type { Num2 } from './types2';
export type Num = Num2;
",
"// temp/types2.d.ts
// temp/types2.d.ts
export type Num2 = number;
",
"// temp/component.d.ts
export declare function Component(): React.JSX.Element;
",
]
"
`;

exports[`rollup > write entry-points (#34) 1`] = `
Expand Down
7 changes: 2 additions & 5 deletions tests/esbuild.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { readdir, readFile } from 'node:fs/promises'
import path from 'node:path'
import { outputToSnapshot } from '@sxzz/test-utils'
import { build } from 'esbuild'
import { describe, expect, test } from 'vitest'
import { dependencies } from '../package.json'
Expand Down Expand Up @@ -43,10 +44,6 @@ describe('esbuild', () => {
format: 'esm',
})

expect(
outputFiles.map(
(file) => `// ${file.path.replaceAll('\\', '/')}\n${file.text}`,
),
).toMatchSnapshot()
expect(outputToSnapshot(outputFiles)).toMatchSnapshot()
})
})
10 changes: 2 additions & 8 deletions tests/rolldown.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'node:path'
import { outputToSnapshot } from '@sxzz/test-utils'
import { rolldown } from 'rolldown'
import { expect, test } from 'vitest'
import UnpluginIsolatedDecl from '../src/rolldown'
Expand All @@ -21,12 +22,5 @@ test('rolldown', async () => {
const result = await bundle.generate({
dir: dist,
})
expect(
result.output
.sort((a, b) => a.fileName.localeCompare(b.fileName))
.map(
(asset) =>
`// ${asset.fileName.replaceAll('\\', '/')}\n${asset.type === 'chunk' ? asset.code : asset.source}`,
),
).toMatchSnapshot()
expect(outputToSnapshot(result.output)).toMatchSnapshot()
})
106 changes: 32 additions & 74 deletions tests/rollup.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,34 @@
import fs from 'node:fs/promises'
import path from 'node:path'
import { outputToSnapshot } from '@sxzz/test-utils'
import { rollup } from 'rollup'
import esbuild from 'rollup-plugin-esbuild'
import { describe, expect, test } from 'vitest'
import UnpluginIsolatedDecl from '../src/rollup'

async function getFileSnapshot(dir: string) {
/**
* Map written output from file system rather than from bundle due to
* module execution order not consistent
*
* @see https://github.com/rollup/rollup/issues/3888
*/
const files = (
await fs.readdir(dir, { recursive: true, withFileTypes: true })
).filter((it) => it.isFile())

const snapshot = await Promise.all(
files.map(async (it) => {
const absolute = path.resolve(it.parentPath, it.name)
const filePath = path.relative(dir, absolute)
const content = await fs.readFile(absolute, 'utf-8')

return `// ${filePath.replaceAll('\\', '/')}\n${content.toString()}`
}),
)
return snapshot
}

describe('rollup', () => {
const TEST_SANDBOX_FOLDER = 'temp/rollup'

Expand All @@ -27,14 +51,7 @@ describe('rollup', () => {
dir: dist,
})

expect(
result.output.map((asset) =>
[
`// ${asset.fileName.replaceAll('\\', '/')}`,
asset.type === 'chunk' ? asset.code : asset.source,
].join('\n'),
),
).toMatchSnapshot()
expect(outputToSnapshot(result.output)).toMatchSnapshot()
})

test('write entry-points', async () => {
Expand All @@ -57,45 +74,14 @@ describe('rollup', () => {
preserveModules: true,
})

/**
* Map written output from file system rather than from bundle due to
* module execution order not consistent
*
* @see https://github.com/rollup/rollup/issues/3888
*/
const allBundledFiles = (
await fs.readdir(dist, {
recursive: true,
withFileTypes: true,
})
).filter((it) => it.isFile())

const fileSystemOutput = allBundledFiles.map((it) => {
return (async () => {
const filePath = path.relative(dist, path.join(it.parentPath, it.name))

const content = await fs.readFile(path.join(dist, filePath), 'utf-8')

return [
`// ${filePath.replaceAll('\\', '/')}`,
content.toString(),
].join('\n')
})()
})

expect(await Promise.all(fileSystemOutput)).toMatchSnapshot()
expect(await getFileSnapshot(dist)).toMatchSnapshot()
})

test('write entry-points (#34)', async () => {
const input =
// [
// path.resolve(__dirname, 'fixtures/entry-points2/index.ts'),
// path.resolve(__dirname, 'fixtures/entry-points2/foo/bar/index.ts'),
// ]
{
index: path.resolve(__dirname, 'fixtures/entry-points2/index.ts'),
bar: path.resolve(__dirname, 'fixtures/entry-points2/foo/bar/index.ts'),
}
const input = {
index: path.resolve(__dirname, 'fixtures/entry-points2/index.ts'),
bar: path.resolve(__dirname, 'fixtures/entry-points2/foo/bar/index.ts'),
}
const dist = path.resolve(__dirname, `${TEST_SANDBOX_FOLDER}/entry-points2`)

const bundle = await rollup({
Expand All @@ -109,36 +95,8 @@ describe('rollup', () => {
logLevel: 'silent',
})

await bundle.write({
dir: dist,
})

/**
* Map written output from file system rather than from bundle due to
* module execution order not consistent
*
* @see https://github.com/rollup/rollup/issues/3888
*/
const allBundledFiles = (
await fs.readdir(dist, {
recursive: true,
withFileTypes: true,
})
).filter((it) => it.isFile())

const fileSystemOutput = allBundledFiles.map((it) => {
return (async () => {
const filePath = path.relative(dist, path.join(it.parentPath, it.name))

const content = await fs.readFile(path.join(dist, filePath), 'utf-8')

return [
`// ${filePath.replaceAll('\\', '/')}`,
content.toString(),
].join('\n')
})()
})
await bundle.write({ dir: dist })

expect(await Promise.all(fileSystemOutput)).toMatchSnapshot()
expect(await getFileSnapshot(dist)).toMatchSnapshot()
})
})

0 comments on commit d5fb870

Please sign in to comment.