ecsstatic icon indicating copy to clipboard operation
ecsstatic copied to clipboard

tests

Open mayank99 opened this issue 2 years ago • 2 comments
trafficstars

currently there is no testing strategy, making it extremely easy for things to break 😳

options to consider:

mayank99 avatar Jan 25 '23 00:01 mayank99

I cloned down and got vitest hooked up quite easily - running vite directly, compiling and comparing snapshots.

Example test
describe('Simple parsing', () => {

	test('Example build', async () => {
		const vite = await import('../node_modules/vite/dist/node');

		const { output } = await vite.build({
			root: resolve(__dirname, 'build-stub'),
			mode: 'production',
			build: { modulePreload: false, write: false, minify: false },
			logLevel: 'silent',
			plugins: [
				ecsstatic(),
				virtualFile('entry.js', `
					import { css } from '@acab/ecsstatic';
					const test = css\`color: blue;\`;
					console.log(test);
				`),
			],
		}) as import('vite').RollupOutput;

		expect(output.find(e => e.name === 'index.css').source).toMatchInlineSnapshot(`
			"
			.🎈-qjmeyb{color: blue}"
		`);

		expect(output.find(e => e.name === 'index').code).toMatchInlineSnapshot(`
			"const __Qjmeyb_acab = \\"\\";
			const test = \\"🎈-qjmeyb\\";
			console.log(test);
			"
		`);

	});
});

Some problems with testing like this:

  • Vite plugins don't get picked up by the optional esbuild processing when it hits a template literals.
  • Tests don't re-run when source is changed (not huge issue)
  • Can't test the CSS itself and how it's applied
  • Nested and escaped back-ticks don't look great for easily reading a test, especially when you add template literals.

stephenwf avatar Jan 26 '23 12:01 stephenwf

hey, thanks for looking into this!

  • Vite plugins don't get picked up by the optional esbuild processing when it hits a template literals.

this might be possible to work around by using a real file instead of virtual file. since you're running vite.build, it should know how to read the file system so you could create a small vite app and build that.

  • Can't test the CSS itself and how it's applied

hmm... i think you should be able to read the CSS file produced after vite.build and check its contents?

  • Nested and escaped back-ticks don't look great for easily reading a test, especially when you add template literals.

this should be partly solved by reading a real project.

mayank99 avatar Jan 26 '23 13:01 mayank99