Skip to content

Commit

Permalink
chore: add E2E test for Vite (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
layershifter authored Jan 9, 2024
1 parent b98eae3 commit f986670
Show file tree
Hide file tree
Showing 15 changed files with 621 additions and 512 deletions.
17 changes: 17 additions & 0 deletions configs/ts/aliases.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": {
"paths": {
"@wyw-in-js/object-syntax": ["./examples/object-syntax/src/index.ts"],
"@wyw-in-js/processor-utils": ["./packages/processor-utils/src/index.ts"],
"@wyw-in-js/shared": ["./packages/shared/src/index.ts"],
"@wyw-in-js/transform": ["./packages/transform/src/index.ts"],
"@wyw-in-js/template-tag-syntax": [
"./examples/template-tag-syntax/src/index.ts"
],
"@wyw-in-js/webpack-loader": ["./packages/webpack-loader/src/index.ts"],
"@wyw-in-js/vite": ["./packages/vite/src/index.ts"]
}
},
"display": "Aliases configuration"
}
5 changes: 4 additions & 1 deletion configs/ts/base.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": {
"rootDir": "../..",
"baseUrl": "../..",

"allowSyntheticDefaultImports": true,
"declaration": true,
"esModuleInterop": true,
Expand All @@ -25,5 +28,5 @@
},
"exclude": ["node_modules"],
"display": "Base config",
"extends": "../../tsconfig.aliases.json"
"extends": "./aliases.json"
}
4 changes: 4 additions & 0 deletions e2e/vite/fixture.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.c2tf5um {
color: red;
background: green;
}
5 changes: 5 additions & 0 deletions e2e/vite/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<script src="./src/index.ts" type="module"></script>
</body>
</html>
19 changes: 19 additions & 0 deletions e2e/vite/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "@wyw-in-js/e2e-vite",
"version": "0.1.1",
"dependencies": {
"@wyw-in-js/template-tag-syntax": "workspace:*"
},
"devDependencies": {
"@wyw-in-js/ts-config": "workspace:*",
"@wyw-in-js/vite": "workspace:*",
"picocolors": "^1.0.0",
"tsconfig-paths": "^4.2.0",
"ts-node": "^10.2.9",
"vite": "^5.0.9"
},
"private": true,
"scripts": {
"test": "node -r ts-node/register ./scripts/test.cjs"
}
}
89 changes: 89 additions & 0 deletions e2e/vite/scripts/test.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// @ts-check

const fs = require('node:fs/promises');
const path = require('node:path');
const colors = require('picocolors');
const prettier = require('prettier');
const { build } = require('vite');
const wyw = require('@wyw-in-js/vite');

const PKG_DIR = path.resolve(__dirname, '..');
const DIST_DIR = path.resolve(PKG_DIR, 'dist');

/**
* @param {string} value
* @returns {string}
*/
function normalizeLineEndings(value) {
return value.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
}

async function buildArtefact() {
await build({
build: {
manifest: true,
outDir: DIST_DIR,
},
configFile: false,
plugins: [wyw.default()],
});
}

async function getCSSFromManifest() {
const manifestPath = path.resolve(DIST_DIR, '.vite', 'manifest.json');
const manifest = require(manifestPath);

if (!manifest['index.html']) {
throw new Error('No index.html in manifest');
}

if (!manifest['index.html'].css) {
throw new Error('No CSS in manifest');
}

if (manifest['index.html'].css.length !== 1) {
throw new Error('More than one CSS in manifest');
}

const cssFilePath = path.resolve(DIST_DIR, manifest['index.html'].css[0]);
const cssSnapshot = await fs.readFile(cssFilePath, 'utf-8');

return prettier.format(cssSnapshot, {
parser: 'css',
});
}

async function main() {
console.log(colors.blue('Package directory:'), PKG_DIR);

try {
await fs.rm(DIST_DIR, { recursive: true });
} catch (err) {}

await buildArtefact();

const cssOutput = normalizeLineEndings(await getCSSFromManifest());
const cssFixture = normalizeLineEndings(
await fs.readFile(path.resolve(PKG_DIR, 'fixture.css'), 'utf-8')
);

if (cssOutput !== cssFixture) {
console.log(colors.red('Output CSS:'));
console.log(cssOutput);
console.log(colors.red('Expected CSS:'));
console.log(cssFixture);

throw new Error('CSS output does not match fixture');
}
}

main().then(
() => {
console.log(colors.green('✅ Vite E2E test passed'));
process.exit(0);
},
(error) => {
console.error(colors.red('Error:'), error);
process.exit(1);
}
);
8 changes: 8 additions & 0 deletions e2e/vite/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { css } from '@wyw-in-js/template-tag-syntax';

const classA = css`
color: red;
background: green;
`;

export { classA };
14 changes: 14 additions & 0 deletions e2e/vite/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "@wyw-in-js/ts-config/node.json",
"files": [],
"compilerOptions": {
"allowJs": true,
"rootDir": "../..",
"baseUrl": "../.."
},
"include": ["./src/*", "./scripts/*"],
"ts-node": {
"transpileOnly": true,
"require": ["tsconfig-paths/register"]
}
}
1 change: 1 addition & 0 deletions examples/object-syntax/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@wyw-in-js/ts-config": "workspace:*",
"dedent": "^1.5.1"
},
"main": "src/index.ts",
"private": true,
"scripts": {
"build:esm": "babel src --out-dir esm --extensions '.js,.jsx,.ts,.tsx' --source-maps --delete-dir-on-start",
Expand Down
5 changes: 0 additions & 5 deletions examples/object-syntax/processors/makeStyles.js

This file was deleted.

1 change: 1 addition & 0 deletions examples/template-tag-syntax/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@wyw-in-js/ts-config": "workspace:*",
"dedent": "^1.5.1"
},
"main": "src/index.ts",
"private": true,
"scripts": {
"build:esm": "babel src --out-dir esm --extensions '.js,.jsx,.ts,.tsx' --source-maps --delete-dir-on-start",
Expand Down
5 changes: 0 additions & 5 deletions examples/template-tag-syntax/processors/css.js

This file was deleted.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@
"scripts": {
"build": "turbo run build:esm build:lib build:types",
"check": "turbo run lint test",
"clean": "del 'packages/*/{coverage,dist,esm,lib,types,tsconfig.tsbuildinfo}'",
"idea-exclude:artifacts": "idea-exclude artifacts \"{configs,examples,packages}/*/{.turbo,esm,lib,types}\"",
"clean": "del '{e2e,examples,packages}/*/{coverage,dist,esm,lib,types,tsconfig.tsbuildinfo}'",
"idea-exclude:artifacts": "idea-exclude artifacts \"{configs,e2e,examples,packages}/*/{.turbo,esm,lib,types}\"",
"idea-exclude:nm": "idea-exclude node_modules \"**/node_modules\"",
"lint": "eslint --ext .js,.ts,.mjs scripts",
"release": "pnpm build && pnpm changeset publish",
Expand Down
Loading

0 comments on commit f986670

Please sign in to comment.