Skip to content

Commit 257b423

Browse files
initial commit 🚀
1 parent 3f0b993 commit 257b423

File tree

10 files changed

+3589
-1
lines changed

10 files changed

+3589
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

README.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,32 @@
1-
# api-test-pattern
1+
# Project Name
2+
3+
In this project I'll show how we can use the power of typescript when we use Jest and Supertest to test the api's application.
4+
5+
## Table of Contents
6+
7+
- [Project Name](#project-name)
8+
- [Table of Contents](#table-of-contents)
9+
- [Installation](#installation)
10+
- [Usage](#usage)
11+
- [Contact](#contact)
12+
13+
## Installation
14+
15+
Run the bellow sentences to install and use the project:
16+
17+
`npm install`
18+
Or
19+
`npm i ts-jest`
20+
`npm i @types/supertest`
21+
22+
## Usage
23+
24+
After you install the depencencies you can use the command bellow to run the tests:
25+
26+
`npm test`
27+
28+
## Contact
29+
30+
If you have interested to know more about it, you can feel free to contact me througt linkedin!
31+
32+
www.linkedin.com/in/alexalexandrealves

constants/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const BASE_URL = 'https://fakerestapi.azurewebsites.net/'

custom-sequencer.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const Sequencer = require('@jest/test-sequencer').default;
2+
3+
class CustomSequencer extends Sequencer {
4+
/**
5+
* Select tests for shard requested via --shard=shardIndex/shardCount
6+
* Sharding is applied before sorting
7+
*/
8+
shard(tests, {shardIndex, shardCount}) {
9+
const shardSize = Math.ceil(tests.length / shardCount);
10+
const shardStart = shardSize * (shardIndex - 1);
11+
const shardEnd = shardSize * shardIndex;
12+
13+
return [...tests]
14+
.sort((a, b) => (a.path > b.path ? 1 : -1))
15+
.slice(shardStart, shardEnd);
16+
}
17+
18+
/**
19+
* Sort test to determine order of execution
20+
* Sorting is applied after sharding
21+
*/
22+
sort(tests) {
23+
// Test structure information
24+
// https://github.com/jestjs/jest/blob/6b8b1404a1d9254e7d5d90a8934087a9c9899dab/packages/jest-runner/src/types.ts#L17-L21
25+
const copyTests = Array.from(tests);
26+
return copyTests.sort((testA, testB) => (testA.path > testB.path ? 1 : -1));
27+
}
28+
}
29+
30+
module.exports = CustomSequencer;

jest.config.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import type { Config } from 'jest';
2+
3+
const config: Config = {
4+
preset: 'ts-jest', // Define o preset do jest
5+
testEnvironment: 'node', // Define o ambiente de teste
6+
testSequencer: './custom-sequencer.js', // Define o sequenciador de testes
7+
maxWorkers: 1, // Define a quantidade de workers simultâneos
8+
// testPathPattern: '.*.spec.ts', // Define o padrão de busca dos arquivos de teste
9+
// globalSetup: Define o setup global de configuração
10+
// modulePathIgnorePatterns: [] Ignora os arquivos que estão no caminho
11+
transform: {
12+
'^.+\\.tsx?$': ['ts-jest', {
13+
diagnostics: {
14+
ignoreCodes: ['TS151001'],
15+
},
16+
}],
17+
},
18+
reporters: [
19+
"default",
20+
["./node_modules/jest-html-reporter", {
21+
"pageTitle": "Test Report"
22+
}]
23+
],
24+
testLocationInResults: true
25+
};
26+
27+
export default config;

0 commit comments

Comments
 (0)