Skip to content

Commit 92306fa

Browse files
committed
Add tests
1 parent 35873ff commit 92306fa

File tree

3 files changed

+268
-28
lines changed

3 files changed

+268
-28
lines changed

jest.config.js

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
// For a detailed explanation regarding each configuration property, visit:
2+
// https://jestjs.io/docs/en/configuration.html
3+
4+
module.exports = {
5+
// All imported modules in your tests should be mocked automatically
6+
// automock: false,
7+
8+
// Stop running tests after `n` failures
9+
// bail: 0,
10+
11+
// The directory where Jest should store its cached dependency information
12+
// cacheDirectory: "C:\\Users\\juan-\\AppData\\Local\\Temp\\jest",
13+
14+
// Automatically clear mock calls and instances between every test
15+
clearMocks: true,
16+
17+
// Indicates whether the coverage information should be collected while executing the test
18+
// collectCoverage: false,
19+
20+
// An array of glob patterns indicating a set of files for which coverage information should be collected
21+
// collectCoverageFrom: undefined,
22+
23+
// The directory where Jest should output its coverage files
24+
coverageDirectory: "coverage",
25+
26+
// An array of regexp pattern strings used to skip coverage collection
27+
// coveragePathIgnorePatterns: [
28+
// "\\\\node_modules\\\\"
29+
// ],
30+
31+
// Indicates which provider should be used to instrument code for coverage
32+
coverageProvider: "v8",
33+
34+
// A list of reporter names that Jest uses when writing coverage reports
35+
// coverageReporters: [
36+
// "json",
37+
// "text",
38+
// "lcov",
39+
// "clover"
40+
// ],
41+
42+
// An object that configures minimum threshold enforcement for coverage results
43+
// coverageThreshold: undefined,
44+
45+
// A path to a custom dependency extractor
46+
// dependencyExtractor: undefined,
47+
48+
// Make calling deprecated APIs throw helpful error messages
49+
// errorOnDeprecated: false,
50+
51+
// Force coverage collection from ignored files using an array of glob patterns
52+
// forceCoverageMatch: [],
53+
54+
// A path to a module which exports an async function that is triggered once before all test suites
55+
// globalSetup: undefined,
56+
57+
// A path to a module which exports an async function that is triggered once after all test suites
58+
// globalTeardown: undefined,
59+
60+
// A set of global variables that need to be available in all test environments
61+
// globals: {},
62+
63+
// The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
64+
// maxWorkers: "50%",
65+
66+
// An array of directory names to be searched recursively up from the requiring module's location
67+
// moduleDirectories: [
68+
// "node_modules"
69+
// ],
70+
71+
// An array of file extensions your modules use
72+
// moduleFileExtensions: [
73+
// "js",
74+
// "json",
75+
// "jsx",
76+
// "ts",
77+
// "tsx",
78+
// "node"
79+
// ],
80+
81+
// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
82+
// moduleNameMapper: {},
83+
84+
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
85+
// modulePathIgnorePatterns: [],
86+
87+
// Activates notifications for test results
88+
// notify: false,
89+
90+
// An enum that specifies notification mode. Requires { notify: true }
91+
// notifyMode: "failure-change",
92+
93+
// A preset that is used as a base for Jest's configuration
94+
// preset: undefined,
95+
96+
// Run tests from one or more projects
97+
// projects: undefined,
98+
99+
// Use this configuration option to add custom reporters to Jest
100+
// reporters: undefined,
101+
102+
// Automatically reset mock state between every test
103+
// resetMocks: false,
104+
105+
// Reset the module registry before running each individual test
106+
// resetModules: false,
107+
108+
// A path to a custom resolver
109+
// resolver: undefined,
110+
111+
// Automatically restore mock state between every test
112+
// restoreMocks: false,
113+
114+
// The root directory that Jest should scan for tests and modules within
115+
// rootDir: undefined,
116+
117+
// A list of paths to directories that Jest should use to search for files in
118+
// roots: [
119+
// "<rootDir>"
120+
// ],
121+
122+
// Allows you to use a custom runner instead of Jest's default test runner
123+
// runner: "jest-runner",
124+
125+
// The paths to modules that run some code to configure or set up the testing environment before each test
126+
// setupFiles: [],
127+
128+
// A list of paths to modules that run some code to configure or set up the testing framework before each test
129+
// setupFilesAfterEnv: [],
130+
131+
// The number of seconds after which a test is considered as slow and reported as such in the results.
132+
// slowTestThreshold: 5,
133+
134+
// A list of paths to snapshot serializer modules Jest should use for snapshot testing
135+
// snapshotSerializers: [],
136+
137+
// The test environment that will be used for testing
138+
testEnvironment: "node",
139+
140+
// Options that will be passed to the testEnvironment
141+
// testEnvironmentOptions: {},
142+
143+
// Adds a location field to test results
144+
// testLocationInResults: false,
145+
146+
// The glob patterns Jest uses to detect test files
147+
// testMatch: [
148+
// "**/__tests__/**/*.[jt]s?(x)",
149+
// "**/?(*.)+(spec|test).[tj]s?(x)"
150+
// ],
151+
152+
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
153+
// testPathIgnorePatterns: [
154+
// "\\\\node_modules\\\\"
155+
// ],
156+
157+
// The regexp pattern or array of patterns that Jest uses to detect test files
158+
// testRegex: [],
159+
160+
// This option allows the use of a custom results processor
161+
// testResultsProcessor: undefined,
162+
163+
// This option allows use of a custom test runner
164+
// testRunner: "jasmine2",
165+
166+
// This option sets the URL for the jsdom environment. It is reflected in properties such as location.href
167+
// testURL: "http://localhost",
168+
169+
// Setting this value to "fake" allows the use of fake timers for functions such as "setTimeout"
170+
// timers: "real",
171+
172+
// A map from regular expressions to paths to transformers
173+
// transform: undefined,
174+
175+
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
176+
// transformIgnorePatterns: [
177+
// "\\\\node_modules\\\\",
178+
// "\\.pnp\\.[^\\\\]+$"
179+
// ],
180+
181+
// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
182+
// unmockedModulePathPatterns: undefined,
183+
184+
// Indicates whether each individual test should be reported during the run
185+
// verbose: undefined,
186+
187+
// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
188+
// watchPathIgnorePatterns: [],
189+
190+
// Whether to use watchman for file crawling
191+
// watchman: true,
192+
};

package.json

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
{
2-
"name": "api-restful-sign-in-up",
3-
"version": "1.0.0",
4-
"description": "Restful Login API created for a selection process.",
5-
"main": "index.js",
6-
"scripts": {
7-
"start": "node src/server.js",
8-
"dev": "nodemon -r dotenv/config src/server.js --ignore tests",
9-
"test": "echo \"Error: no test specified\" && exit 1"
10-
},
11-
"author": "Juan Patrick",
12-
"license": "ISC",
13-
"dependencies": {
14-
"bcryptjs": "^2.4.3",
15-
"cors": "^2.8.5",
16-
"eslint": "^7.11.0",
17-
"express": "^4.17.1",
18-
"jest": "^26.5.3",
19-
"jsonwebtoken": "^8.5.1",
20-
"mongoose": "^5.10.9"
21-
},
22-
"devDependencies": {
23-
"dotenv": "^8.2.0",
24-
"eslint-config-airbnb-base": "^14.2.0",
25-
"eslint-config-prettier": "^6.12.0",
26-
"eslint-plugin-prettier": "^3.1.4",
27-
"nodemon": "^2.0.5",
28-
"supertest": "^5.0.0"
29-
}
2+
"name": "api-restful-sign-in-up",
3+
"version": "1.0.0",
4+
"description": "Restful Login API created for a selection process.",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "node src/server.js",
8+
"dev": "nodemon -r dotenv/config src/server.js --ignore tests",
9+
"test": "jest --setupFiles dotenv/config --detectOpenHandles --forceExit",
10+
"test:clear": "jest --clearCache"
11+
},
12+
"author": "Juan Patrick",
13+
"license": "ISC",
14+
"dependencies": {
15+
"bcryptjs": "^2.4.3",
16+
"cors": "^2.8.5",
17+
"eslint": "^7.11.0",
18+
"express": "^4.17.1",
19+
"jest": "^26.5.3",
20+
"jsonwebtoken": "^8.5.1",
21+
"mongoose": "^5.10.9",
22+
"yup": "^0.29.3"
23+
},
24+
"devDependencies": {
25+
"dotenv": "^8.2.0",
26+
"eslint-config-airbnb-base": "^14.2.0",
27+
"eslint-config-prettier": "^6.12.0",
28+
"eslint-plugin-prettier": "^3.1.4",
29+
"nodemon": "^2.0.5",
30+
"supertest": "^5.0.0"
31+
}
3032
}

tests/api.test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const request = require('supertest');
2+
const app = require('../src/app');
3+
4+
describe('Create', () => {
5+
it('should create user session', async (done) => {
6+
const response = await request(app)
7+
.post('/users')
8+
.send({
9+
nome: 'Juan Patrick',
10+
email: `juan${Math.floor(Math.random() * 9999) + 1}@gmail.com`,
11+
senha: '123123123',
12+
telefones: [
13+
{
14+
numero: '951415258',
15+
ddd: '11',
16+
},
17+
],
18+
});
19+
20+
expect(response.status).toBe(201);
21+
done();
22+
});
23+
});
24+
25+
describe('Authentication', () => {
26+
it('should create session authentication', async (done) => {
27+
const response = await request(app).post('/sign-in').send({
28+
29+
senha: '1234',
30+
});
31+
32+
expect(response.status).toBe(200);
33+
done();
34+
});
35+
});
36+
37+
describe('User', () => {
38+
it('should list user by id', async (done) => {
39+
const response = await request(app)
40+
.get('/users/5f877dc25d9f7b5c08f77c16')
41+
.send();
42+
43+
expect(response.status).toBe(401);
44+
done();
45+
});
46+
});

0 commit comments

Comments
 (0)