Skip to content

GitLab

  • Projects
  • Groups
  • Snippets
  • Help
    • Loading...
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in
js-frontend-tutorial js-frontend-tutorial
  • Project overview
    • Project overview
    • Details
    • Activity
    • Releases
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 0
    • Issues 0
    • List
    • Boards
    • Labels
    • Service Desk
    • Milestones
  • Merge requests 0
    • Merge requests 0
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Operations
    • Operations
    • Incidents
    • Environments
  • Packages & Registries
    • Packages & Registries
    • Container Registry
  • Analytics
    • Analytics
    • CI/CD
    • Repository
    • Value Stream
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Members
    • Members
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • ci-general
  • js-frontend-tutorialjs-frontend-tutorial
  • Wiki
  • Configure unit tests on local machine

Last edited by Taras Gaidukov Jul 11, 2019
Page history
This is an old version of this page. You can view the most recent version or browse the history.

Configure unit tests on local machine

Configure unit tests on local machine

https://jasmine.github.io/
https://karma-runner.github.io/latest/index.html

Write unit tests

For writing unit tests we will use the Jasmine framework.

  1. Initialize NodeJS project with npm init and install jasmine:
npm init
npm install --save-dev jasmine
  1. Unit tests are located in *.spec.js files. Create spec file with unit tests for sum.js, in this tutorial we will test the sum function only:

js/sum.spec.js

describe('Sample project', () => {
    it('sum should return 3', () => {
        const result = sum(1, 2);
        expect(result).toBe(3);
    });

    it('sum should return 0', () => {
        const result = sum(1, -1);
        expect(result).toBe(0);
    });
});
  1. Install test runner named Karma with Chrome launcher and Jasmin plugin:
npm install --save-dev karma karma-jasmine karma-chrome-launcher
  1. Create karma configuration file:

karma.conf.js

module.exports = config => {
    config.set({
        basePath: './js',
        frameworks: ['jasmine'],
        files: [
            '**/*.js'
        ],
        exclude: [
            'main.js'
        ],
        preprocessors: {},
        reporters: ['progress'],
        port: 9876,
        logLevel: config.LOG_INFO,
        autoWatch: true,
        browsers: ['Chrome'],
        singleRun: false,
        concurrency: Infinity
    });
};
  1. Add test command into package.json file:
"scripts": {
  "test": "karma start karma.conf.js"
}
  1. Run tests:
npm run test
Clone repository
  • A sample project
  • Build docker image
  • Configure GitLab CI
  • Configure e2e tests for gitlab runner
  • Configure e2e tests on local machine
  • Configure unit tests on local machine
  • Confirure unit tests for gitlab runner
  • Register gitlab runner
  • What is Continuous Integration
  • Home