You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesi贸n a prueba de IA

Antes: $249

Currency
$209
Suscr铆bete

Termina en:

2 D铆as
12 Hrs
2 Min
46 Seg

Configurando el entorno de pruebas para nuestro proyecto

7/25
Resources

How to modularize applications for development and test environments?

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.

What aspects to consider for separating responsibilities?

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.

  1. Setup: This is the stage where all the application configuration such as routers, middlewares and security settings are set up.
  2. Listening: This is the part where the application starts receiving requests on a specific port.

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;

How do we structure the main application for production and testing?

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}`);});

How to create test endpoints in a real application?

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.

  1. Define the endpoint: You can define a simple endpoint such as /hello and specify the response it should generate.
// Example of endpoint creationapp.get('/hello', (req, res) => { res.status(200).json({ name: "Nico" }); });
  1. Test the endpoint: Use tools like Postman or Insomnia to verify that the endpoint responds correctly with the expected name and status.

What will you learn in the next sessions?

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

Sort by:

Want to see more contributions, questions and answers from the community?

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
Ojo para las personas que hagan pruebas con archivos que sean .mjs (modules de emmac script) van a tener que configurar varias cosas diferentes, les comparto la configuraci贸n que realice yo jest-config.json `{聽 "testEnvironment": "node",聽 "moduleFileExtensions": ["js", "mjs", "json"],聽 "testRegex": ".e2e.(js|mjs)$",聽 "transform": {},聽 "transformIgnorePatterns": []` `}` en el package.json `"e2e": "node --experimental-vm-modules node_modules/jest/bin/jest.js --config ./jest-e2e.config.json --verbose 聽e2e/"`