Search code examples
node.jsunit-testingjestjsnunithapi.js

How do I separate between integration testing and unit testing using Hapi in Node.js?


How do I separate between integration testing and unit testing using Hapi in Node.js?

I am trying to reduce the testing time in our automations for every time we update a unit within our API.

I was wondering can I just create two new folders test/unit and test/int to separate the scripts and what would the update to the package.json require?

I have the following in the package.json which runs the .labrc.js package.json

    "test": "npm run version-stamp && lab --bail -t 80 -r html -o coverage.html -r console -o stdout -r junit -o TestResult.xml -r lcov -o coverage.dat",
    "simple-test": "npm run version-stamp && lab",
    "test-int": "not sure what to put here",

labrc.js

module.exports = {
    verbose: true,
    timeout: 7500,
    lint: true,
    paths: [
        "test/init/aws",
        "test/unit/plugins",
        "test/unit/utils",
        "test/unit/routes"
    ],

Solution

  • Sure your approach sounds ok. Issue is that lab don't allow custom labrc to specify different paths for integration tests. but you can try to do something like this

    Separate tests in two different directories

    • test/unit
    • test/integration

    Provide an additional alternative labrc.js config file for integration tests and remove the one available in the root directory,

    You will have two different labrc.js files

    • test/unit/labrc.js
    • test/integration/labrc.js

    The one for Unit Test should look like

    test/unit/labrc.js

    module.exports = {
        verbose: true,
        timeout: 7500,
        lint: true,
        paths: [
            "unit/plugins",
            "unit/utils",
            "unit/routes",
        ],
    

    For Integration Tests

    test/unit/labrc.js

    module.exports = {
        verbose: true,
        timeout: 7500,
        lint: true,
        paths: [
            "integration/aws",
        ],
    

    In package.json

    {
        ... Lot of JSON config here
        "test": "npm run version-stamp && lab test/unit --bail -t 80 -r html -o coverage.html -r console -o stdout -r junit -o TestResult.xml -r lcov -o coverage.dat",
        "simple-test": "npm run version-stamp && lab test/unit",
        "test-integration": "lab test/integration", 
        ... Further JSON config here
    }
    

    Customize the parameters passed to the lab

    As a Suggestion prefer integration over int It may be confusing.