From 282745bcff2c08fcad263f2029a3820170e3318f Mon Sep 17 00:00:00 2001 From: Marco Pasqualetti Date: Sun, 15 Dec 2024 15:40:00 +0100 Subject: [PATCH] test: windows always brings joy --- .../tests/generator.spec.ts | 2 +- .../sources/dynamic-only/client.expected.tsx | 2 +- .../sources/dynamic-only/server.expected.tsx | 2 +- .../tests/sources/dynamic-only/source.tsx | 4 +- .../tests/transpileSource.test.ts | 41 ++++++++++++------- 5 files changed, 32 insertions(+), 19 deletions(-) diff --git a/packages/fs-router-vite-plugin/tests/generator.spec.ts b/packages/fs-router-vite-plugin/tests/generator.spec.ts index fddf73ad..efa7909d 100644 --- a/packages/fs-router-vite-plugin/tests/generator.spec.ts +++ b/packages/fs-router-vite-plugin/tests/generator.spec.ts @@ -5,7 +5,7 @@ import { describe, it, expect } from 'vitest' import { routeGenerator } from '../src/generator' describe('generator works', async () => { - const folderNames = await fs.readdir(process.cwd() + '/tests/generator') + const folderNames = await fs.readdir(`${process.cwd()}/tests/generator`) it.each(folderNames)( 'should wire-up the routes for a "%s" tree', diff --git a/packages/lazy-fn-vite-plugin/tests/sources/dynamic-only/client.expected.tsx b/packages/lazy-fn-vite-plugin/tests/sources/dynamic-only/client.expected.tsx index 92726887..87cbe459 100644 --- a/packages/lazy-fn-vite-plugin/tests/sources/dynamic-only/client.expected.tsx +++ b/packages/lazy-fn-vite-plugin/tests/sources/dynamic-only/client.expected.tsx @@ -1,7 +1,7 @@ import React, { Suspense, type JSX } from "react"; import { __tuono__internal__lazyLoadComponent as dynamic } from "tuono"; const DynamicComponent = dynamic(() => import("../components/DynamicComponent")); -const Loading = () => <>Loading; +const Loading = (): JSX.Element => <>Loading; export default function IndexPage(): JSX.Element { return }> diff --git a/packages/lazy-fn-vite-plugin/tests/sources/dynamic-only/server.expected.tsx b/packages/lazy-fn-vite-plugin/tests/sources/dynamic-only/server.expected.tsx index 1cc6ad9b..91c82608 100644 --- a/packages/lazy-fn-vite-plugin/tests/sources/dynamic-only/server.expected.tsx +++ b/packages/lazy-fn-vite-plugin/tests/sources/dynamic-only/server.expected.tsx @@ -1,7 +1,7 @@ import React, { Suspense, type JSX } from "react"; import "tuono"; import DynamicComponent from "../components/DynamicComponent"; -const Loading = () => <>Loading; +const Loading = (): JSX.Element => <>Loading; export default function IndexPage(): JSX.Element { return }> diff --git a/packages/lazy-fn-vite-plugin/tests/sources/dynamic-only/source.tsx b/packages/lazy-fn-vite-plugin/tests/sources/dynamic-only/source.tsx index fd52e08c..98981ad8 100644 --- a/packages/lazy-fn-vite-plugin/tests/sources/dynamic-only/source.tsx +++ b/packages/lazy-fn-vite-plugin/tests/sources/dynamic-only/source.tsx @@ -1,9 +1,9 @@ import React, { Suspense, type JSX } from "react"; import { dynamic } from "tuono"; -const DynamicComponent = dynamic(() => import("../components/DynamicComponent")); +const DynamicComponent = dynamic(() => import("../components/DynamicComponent")) -const Loading = () => <>Loading; +const Loading = (): JSX.Element => <>Loading export default function IndexPage(): JSX.Element { return ( diff --git a/packages/lazy-fn-vite-plugin/tests/transpileSource.test.ts b/packages/lazy-fn-vite-plugin/tests/transpileSource.test.ts index 6ffcb33e..c1a6b8f3 100644 --- a/packages/lazy-fn-vite-plugin/tests/transpileSource.test.ts +++ b/packages/lazy-fn-vite-plugin/tests/transpileSource.test.ts @@ -1,5 +1,5 @@ import fs from 'node:fs/promises' -import path from 'node:path' +import os from 'node:os' import { it, expect, describe } from 'vitest' import type { Plugin } from 'vite' @@ -17,35 +17,48 @@ function getTransform(): (...args: Parameters) => string { return LazyLoadingPlugin().transform as never } -describe('"dynamic" fn', async () => { +describe('"dynamic" sources', async () => { const folderNames = await fs.readdir(`${process.cwd()}/tests/sources`) - it.each(folderNames)( - 'should correctly build the "%s" dynamic fn', - async (folderName) => { - const testDirPath = `${process.cwd()}/tests/sources/${folderName}` + describe.each(folderNames)('%s', async (folderName) => { + const testDirPath = `${process.cwd()}/tests/sources/${folderName}` - const source = await fs.readFile( - path.join(testDirPath, 'source.tsx'), - 'utf-8', - ) + const sourceRaw = await fs.readFile(`${testDirPath}/source.tsx`, 'utf-8') + /** + * When adding `packages/lazy-fn-vite-plugin/tests/sources/dynamic-only` only + * the test involving that fixture were broken on Windows... but not the one in the other fixtures: + * - packages/lazy-fn-vite-plugin/tests/sources/vanilla + * - packages/lazy-fn-vite-plugin/tests/sources/external-dynamic + * + * Awkwardly this doesn't happen on `packages/fs-router-vite-plugin/tests/generator.spec.ts` + * + * Too much pain and sadness to investigate this right now. + * Might worth creating an utility function in the future if this happens again + */ + const source = sourceRaw.replace(new RegExp(os.EOL, 'g'), '\n') + it('should generate file for client', async () => { const pluginTransform = getTransform() const clientBundle = pluginTransform(source, 'id') - const serverBundle = pluginTransform(source, 'id', { ssr: true }) const expectedClientSrc = `${testDirPath}/client.expected.tsx` - const expectedServerSrc = `${testDirPath}/server.expected.tsx` await expect(clientBundle).toMatchFileSnapshot( expectedClientSrc, `${testDirPath} client build should be equal to ${expectedClientSrc}`, ) + }) + + it('should generate file for server', async () => { + const pluginTransform = getTransform() + const serverBundle = pluginTransform(source, 'id', { ssr: true }) + + const expectedServerSrc = `${testDirPath}/server.expected.tsx` await expect(serverBundle).toMatchFileSnapshot( expectedServerSrc, `${testDirPath} server build should be equal to ${expectedServerSrc}`, ) - }, - ) + }) + }) })