Master automated testing in CI/CD pipelines. Build applications, run test suites, generate coverage reports, and use matrix strategies for cross-platform testing.
Set up a Node.js application with a proper test suite.
mkdir cicd-testing && cd cicd-testingnpm init -ynpm install --save expressnpm install --save-dev jest supertestcat > app.js << 'EOF'
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.json({ message: 'Hello CI/CD!' });
});
app.get('/health', (req, res) => {
res.json({ status: 'healthy' });
});
module.exports = app;
EOFcat > server.js << 'EOF'
const app = require('./app');
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
EOFcat > app.test.js << 'EOF'
const request = require('supertest');
const app = require('./app');
describe('API Endpoints', () => {
test('GET / returns message', async () => {
const response = await request(app).get('/');
expect(response.status).toBe(200);
expect(response.body.message).toBe('Hello CI/CD!');
});
test('GET /health returns healthy status', async () => {
const response = await request(app).get('/health');
expect(response.status).toBe(200);
expect(response.body.status).toBe('healthy');
});
});
EOFnpx json -I -f package.json -e 'this.scripts.test="jest"'npx json -I -f package.json -e 'this.scripts.start="node server.js"'npm testWe create a REST API with two endpoints and comprehensive tests. The app.js exports the Express app separately from server.js, making it testable with supertest.
Tests pass successfully. You'll see '2 passed' in the test output. This proves our application works before we automate it in CI.