37
loading...
This website collects cookies to deliver better user experience
Testing software is essential to the development cycle of building applications. A strong test suite gives the developer(s) a sense of freedom to organize and refactor the codebase as needed without fear of unknowingly breaking something.
HTTP/1.1 201 OK
Content-Type: application/json; charset=utf-8
{
"data": {
"id": "e6f36a",
"title": "Liverpool FC",
"authorId": "5303d74c64f66366f00cb9b2a94f3251bf5",
"tags": ["football", "sport", "Liverpool"],
"url": "https://medium.com/@majelbstoat/liverpool-fc-e6f36a",
"canonicalUrl": "http://jamietalbot.com/posts/liverpool-fc",
"publishStatus": "public",
"publishedAt": 1442286338435,
"license": "all-rights-reserved",
"licenseUrl": "https://medium.com/policy/9db0094a1e0f"
}
// src/mocks/handlers.ts
import { rest } from 'msw'
export const handlers = [
1. rest.post('https://api.medium.com/*', (req, res, ctx) => {
return res(
2. ctx.status(201),
3. ctx.json({
data: {
id: 'e6f36a',
title: 'Liverpool FC',
authorId: '5303d74c64f66366f00cb9b2a94f3251bf5',
tags: ['football', 'sport', 'Liverpool'],
url: 'https://medium.com/@majelbstoat/liverpool-fc-e6f36a',
canonicalUrl: 'http://jamietalbot.com/posts/liverpool-fc',
publishStatus: 'public',
publishedAt: 1442286338435,
license: 'all-rights-reserved',
licenseUrl: 'https://medium.com/policy/9db0094a1e0f',
},
})
)
}),
]
https://api.medium.com/*
.*
represents a wildcard meaning anytime my application attempts to send a POST request to a URL starting with https://api.medium.com/, it will intercept it and return the provided response at 3.jest.config.ts
file, make sure you have the following:// jest.config.ts
module.exports = {
setupFiles: ['dotenv/config'],
}
src/setupTests.js
.// src/setupTests.js
import { server } from './mocks/server.js'
// Establish API mocking before all tests.
beforeAll(() => server.listen())
// Reset any request handlers that we may add during the tests,
// so they don't affect other tests.
afterEach(() => server.resetHandlers())
// Clean up after the tests are finished.
afterAll(() => server.close())
jest.config.ts
file:// jest.config.ts
module.exports = {
setupFilesAfterEnv: ['<rootDir>/src/setupTests.ts'],
}
const data = matter(readFile('./file.md'))
Error: ENOENT: no such file or directory, open "file"
const data = matter(readFile(__dirname + '/file.md'))