3
The question and doubts below address Node.js, Typescript and Javascript. I don’t know if the concepts below apply to other programming platforms/ languages.
I am studying about automated tests (AT) using Jest and Typescript and I have come up with a question about the appointment of the test files. In some projects I have analyzed, some files are being named differently. Some have the nomenclature .test.ts
and others .spec.ts
. Until the present moment of the publication of this question, I had in mind that test
or spec
had no semantic difference, both served only to identify that that file is a test file, but the distinct file appointments gave rise to doubt on the subject.
Studying and researching on AT, I found good answers on the subject that helped me to understand a lot of the concept, but nothing that would clarify my doubt. For example, this excellent response from @utluiz where he talks about test concepts, I highlight 2 following points:
-
Unit Test: tests specific parts of the system, such as classes and methods.
-
Integration Testing: Tests multiple components of a system running at once.
Let’s keep that statement in mind.
Now we go from code to exemplify my doubt. In the test folder, I have 2 test files that basically what they do is to test the feature of registering a new administrator on the system. Now let’s go to the first file and I’ll still highlight the file name:
- The first is called
admin-signup.test.ts
and in this test file, he injects mocks of dependencies into the service module (AdminServices
):
import * as AdminServices from '@naointeressa';
const adminData = {...}
const AdminRepositoryMock = {...}
const validatorMock = {...}
const bcryptMockValidPass = {...}
describe('any', () => {
it('should ...', async () => {
const sut = await AdminServices.createAdminService(
AdminRepositoryMock,
validatorMock,
bcryptMockValidPass
)
const newAdmin = await sut.createAdmin(adminData)
expect(newAdmin.name).toBeDefined()
expect(newAdmin.email).toBeDefined()
})
...
})
- The second is called
admin-signup.spec.ts
and this differs from the previous because it does not inject mocks, but the true dependencies of the service module (AdminServices
):
import * as AdminServices from '@naointeressa';
import * as AdminRepository from '@naointeressa';
import * as crypto from '@naointeressa';
import validator from '@naointeressa';
describe('any', () => {
it('should ...', async () => {
const sut = await AdminServices.createAdminService(
AdminRepository,
validator,
cryptoObject,
);
const newAdmin = await sut.createAdmin(adminData);
expect(newAdmin.name).toBeDefined();
expect(newAdmin.email).toBeDefined();
});
});
It was in this code (simplified obviously) that I had this doubt regarding the appointment of the test files.
I made an analysis of the above mentioned examples and came to the following thought:
We use
.test.ts
when our test file does not test any external resources of our application (database, external API, HTTP calls, etc...), ie for tests that follow the quote1
from the @utluiz response.We use
.spec.ts
when testing through the application’s external layers (other modules, libraries, databases, etc...), i.e., for citation2
from the @utluiz response. Testing end-2-end would also have this kind of file nomenclature (quoteTeste de Sistema
in @utluiz’s reply, correct me if I’m wrong).
However I’m not sure of my conclusion above. I’m studying more about tests and I still have some questions, so, about my question of nomenclatures, I ask:
There are semantic differences between naming a file as
.test.ts
and.spec.ts
?If the answer to the above question is yes, is my thinking about where to use certain nomenclature correct? If not, and what context should I use either?
Or if there is no semantic difference between the names, I can adopt anyone, and they all have the same purpose... indicate that a file is a test file?
I used to think that spec
comes from something specific, ie if the test tests a very simple and tiny application functionality like for example a function that checks if an email field is not empty (''
) and only, nothing else. But it did not help me to heal the doubt. I would like a definitive answer.
I don’t think the question is related to Typescript to the point of justifying the tag.
– Luiz Felipe
@Luizfelipe I think you do because you have the face of the concept coming from here: https://stackoverflow.com/a/37502922/2241463
– Piovezan
Yeah, @Piovezan, could be.
– Luiz Felipe
@Luizfelipe I felt a certain air upset there :) I do not understand this, I was just curious about the term "spec" being applied to automated tests and went looking for what could be, but I did not go into the research to reach a definitive conclusion.
– Piovezan
@Piovezan, none of that!! : P It was just agreement anyway. :))
– Luiz Felipe