Ahora si ya empez贸 lo bueno
Requisitos
驴Qu茅 necesitas para aprender Testing E2E con Node.js?
Introducci贸n: pruebas e2e con Node.js
Explorando la API
Explorando el c贸digo del proyecto
Instalando y configurando Jest con Node
Tu primer prueba e2e
Buenas pr谩cticas en pruebas E2E
Pruebas a la API de Fake Store
Configurando el entorno de pruebas para nuestro proyecto
Coverage Report
Pruebas a Schemas o DTOs
Pruebas a endpoints GET
Pruebas al Login
Pruebas a rutas con protecci贸n
Pruebas a rutas con accessToken
Pruebas a endpoints POST
Pruebas en Entornos de Desarrollo Avanzados
驴Crear bases de datos solo para pruebas?
Preparando el ambiente de pruebas e2e
Creando un seed de datos manual
Seed de datos con sequelize
Umzug: corriendo los seeds de datos para pruebas e2e
Pruebas a crear categor铆as
Pruebas al endpoint de producto
Pruebas a la paginaci贸n
Mocking y automatizaci贸n
Mocking en Node.js
Automatizacion en GitHub Actions
Pr贸ximos pasos
驴Quieres m谩s cursos de testing?
You don't have access to this class
Keep learning! Join and start boosting your career
When creating complex applications, one of the fundamental skills is to be able to modularize them properly to ensure that testing is performed effectively in an environment that closely simulates production. This process ensures that any changes to the code can be tested before being deployed. Let's explore how to do this step by step.
Separating responsibilities is a good practice in software development that helps keep the code clean and manageable. It also allows the application to run in both normal development and test environments.
To implement this separation, it is common to create a dedicated file containing the function in charge of setup
.
// In app.jsfunction createApp() { const app = express(); app.use(cors()); // Router and middleware setup return app;}module.exports = createApp;
Once the responsibilities are separated, the main application can import this configuration and decide whether it will run in a development or test environment. In a file like index.js
, you simply import the function and create its context:
// In index.jsconst createApp = require('./app');const app = createApp();const PORT =process.env.PORT || 9000;
app.listen(PORT, () => { console.log(`Serverrunning on port ${PORT}`);});
To validate that our tests are effective, it is important to have test endpoints. If they do not exist, they must be created, following the architecture in which the production application was released.
/hello
and specify the response it should generate.// Example of endpoint creationapp.get('/hello', (req, res) => { res.status(200).json({ name: "Nico" }); });
Now that you've learned how to tune the architecture for testing, the next step is to track test coverage. I will guide you on how to set up reports to measure what is the percentage of test coverage on our endpoints. Let's move forward together towards the E2E testing domain!
Contributions 3
Questions 0
Ahora si ya empez贸 lo bueno
jest.config.js
para typescript
:
/** @type {import('ts-jest').JestConfigWithTsJest} */
function makeModuleNameMapper(srcPath, tsconfigPath) {
// Get paths from tsconfig
const { paths } = require(tsconfigPath).compilerOptions;
const aliases = {};
// Iterate over paths and convert them into moduleNameMapper format
Object.keys(paths).forEach((item) => {
const key = item.replace("/*", "/(.*)");
const path = paths[item][0].replace("/*", "/$1");
aliases[key] = srcPath + "/" + path;
});
return aliases;
}
const TS_CONFIG_PATH = "./tsconfig.json";
const SRC_PATH = "<rootDir>";
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
moduleNameMapper: makeModuleNameMapper(SRC_PATH, TS_CONFIG_PATH),
modulePaths: ["<rootDir>"],
rootDir: ".",
moduleFileExtensions: ["js", "ts"],
};
y en tsconfig.json
agrega:
"esModuleInterop": true
Want to see more contributions, questions and answers from the community?