Currently, I am diving into the world of TDD and attempting to create a basic test suite for my Node Express API.
My project directory has the following structure:
.
└── root/
├── src/
│ ├── services/
│ │ └── MyService.ts
│ └── Server.ts
└── tests/
├── services/
│ └── MyService.spec.ts
└── SetupTests.spec.ts
My goal is to have JEST execute the "SetupTests.spec.ts" first, followed by the "MyService.spec.ts" tests. In my "SetupTests.spec.ts" file, I have the following code:
import Server from '../src/Server'
import axios, { AxiosInstance } from 'axios'
let testAxios: AxiosInstance = axios.create({ baseURL: 'http://localhost:3000' })
let testServer: Server = new Server()
beforeAll(() => {
return testServer.bootstrap() //It returns the 'app.listen()' object
})
afterAll(() => {
return testServer.shutdown()
})
//I'm wondering if it's possible to include other *.spec.ts test files here - is it possible?
However, when I run "npx jest", it attempts to execute "MyService.spec.ts" before "SetupTests.spec.ts", leading to failures since the Server is not listening yet.
What steps can I take to resolve this issue?
Alternatively, should I include the "beforeAll" and "afterAll" calls in every *.spec.ts file? Is this considered a best practice?
Thank you for your assistance!