简体   繁体   中英

Testing 2 or more JavaScript files in 1 Spec.js file?

I was thinking on How can I test 2 or more different javaScript file, example I have 5 javascript files, how can I test those 3 scripts? Do I need to test them all, in 1 spec.js file like this:

javascript1.js

javascript2.js (these 3 to 1 spec file)

javascript3.js

or Can I have them tested on 1:1 ratio like this:

javascript1.js to spec1.js;

javascript2.js to spec2.js;

javascript3.js to spec3.js;

You should create a Spec file for each file you are testing. Sometimes, if a file/module you are testing is extremely complex, it may be an idea to create multiple spec files and bundle them inside a folder.

If you are using Jasmine I would recommend you pick up a technology like Require JS (or webpack or Browserify), which would let you require as many modules/javascript files as you'd like inside spec files and would allow you to keep the scope of your tests limited.

However, if you are in the more traditional approach of jasmine testing with karma on standard namespaced JavaScript, then you'll simply need to configure Karma to load your JavaScript files before the tests are ran -- this way all the files would be available for testing within the testing scope.

For example, inside the karma.conf.js file:

files: [
        {
            pattern: 'src/js/app/libs/**/*.js',
            included: true
        },
        {
            pattern: 'src/js/app/modules/**/*.js',
            included: true
        },
        {
            pattern: 'tests/jasmine/*/*Spec.js',
            included: true
        },
        // below line may not be needed outside of RequireJS scope
        'tests/jasmine/config/karma-call.js' // where I loop for spec files
    ]

For extra reading: http://requirejs.org/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM