Troubleshooting tsup Compilation Error: Excluding Spec Files and Resolving 'vitest' Dependency

I’m working on a project where I like to keep unit tests along with use cases and e2e tests along with controllers, all within the `src/` folder. However, I’m facing an issue with the build script:

```json
“build:prod”: “tsup src --out-dir build”,
```

This script is compiling my `*.spec.ts` files, and when deploying, I get the following error:

✘ [ERROR] Could not resolve “vitest”

This happens because my spec files import `vitest` and `supertest`, which are development dependencies.

To try to resolve this, I created a script called remove-spec-files.ts:

```javascript
import fs from ‘node:fs’
import path from ‘node:path’

function deleteSpecFiles(dirPath: string): void {
const files = fs.readdirSync(dirPath)

for (const file of files) {
const filePath = path.join(dirPath, file)
const stat = fs.lstatSync(filePath)

if (stat.isDirectory()) {
  deleteSpecFiles(filePath)
} else if (filePath.endsWith('.spec.js')) {
  fs.unlinkSync(filePath)
}

}
}

const isProduction = process.env.NODE_ENV === ‘production’
const buildPath = isProduction
? process.cwd()
: path.join(process.cwd(), ‘build’)
deleteSpecFiles(buildPath)
```

After compiling, this script is placed at the root of the build folder and is executed with the following script:

```json
“build:prod:remove-spec-files”: “node remove-spec-files.js”,
```

In Render, I call both scripts like this:

```bash
npm install && npx prisma migrate deploy && npm run build:prod && npm run build:prod:remove-spec-files
```

Even so, I continue to receive the error during deployment:

```
CLI Using tsconfig: tsconfig.json
May 3 10:40:39 AM CLI tsup v6.7.0
May 3 10:40:39 AM CLI Target: es2020
May 3 10:40:39 AM CJS Build start
May 3 10:40:39 AM ✘ [ERROR] Could not resolve “vitest”
```

I would like to ask for help in solving this problem and removing spec files from my `src` folder during the tsup compilation.

I appreciate any help in advance!

Hi,

A quick search for “tsup exclude spec files” brought up this GitHub issue: https://github.com/egoist/tsup/issues/590

It seems you may be able to achieve it with a config line mentioned in this comment: https://github.com/egoist/tsup/issues/590#issuecomment-1265493769, e.g.

export default defineConfig({ entry: ['src', '!src/**/*.spec.*'], // ...})

Alan

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.