typescriptviterollupvitest

Vitest not properly interpreting TypeScript


I have the following setup test:

import {  describe, it } from "vitest";
import { init, initServer } from "../src";
import dotenv from "dotenv";

dotenv.config();

describe("#setup", () => {
  it("should initialize the server", async () => {
    await init(await initServer() as string);
  });
});

Note that cannot use beforeEach of any other before methods because it can only run once on the test command.

However, when I run it I get an error:

[!] RollupError: src/index.ts (22:23): Expected ',', got ':' (Note that you need plugins to import files that are not JavaScript)
src/index.ts (22:23)
20:  * @async
21:  */
22: const init = async (url: string, password?: string) => {
                           ^
23:   const response = await fetch(url);
RollupError: Expected ',', got ':'

I see no reason why this would throw an error is it is valid TS. Here is my Vite config:

import {defineConfig} from "vite";

export default defineConfig({
  server: {
    watch: {
      ignored: ["node_modules", "dist"]
    }
  },
  test: {
    globalSetup: "tests/setup.spec.ts"
  }
});

Vitest workspace:

import { defineWorkspace } from "vitest/config";

export default defineWorkspace([
    {
    extends: "vite.config.ts",
    test: {
        include: ["tests/setup.spec.ts"],
        name: "setup",
        environment: "node" 
    }
}])

Rollup config:

import serve from "rollup-plugin-serve";
import typescript from "@rollup/plugin-typescript";
import copy from "rollup-plugin-copy";

export default [
  {
    input: "src/index.ts",
    output: [
      {
        dir: "dist",
        format: "es"
      }
    ],

    inlineDynamicImports: true,
    presserveModules: true,
    plugins: [
      serve({contentBase: "dist"}),
      typescript(),
      copy({targets: [{src: "src/server/init.sh", dest: "dist"}]})
    ],
    external: ["express"]
  }
];


Solution

  • the order of the plugins in rollup is important, so you should start by typescript() plugin then serving using serve() plugin

        plugins: [
          typescript(),
          serve({contentBase: "dist"}),
          copy({targets: [{src: "src/server/init.sh", dest: "dist"}]})
        ],
    

    I don't know what file you are copying using copy but if it's important for the server or the build, then don't forget that the order is important