如何使用jasmine和browserify进行单元测试?

时间:2022-08-24 21:51:43

Any best way to run the jasmine HTML reporter with browserify styled code? I also want to be able to run this headless with phantomjs, thus the need for the HTML reporter.

使用browserify样式代码运行jasmine HTML报告器的最佳方法是什么?我也希望能够用phantomjs运行这个无头,因此需要HTML记者。

5 个解决方案

#1


4  

I don't think there's a jasmine-browserify package yet, and it doesn't really match Browserify/NPM's way of doing things (avoid global exports).

我认为还没有一个jasmine-browserify软件包,它并不真正符合Browserify / NPM的做事方式(避免全球出口)。

For now, I just include /node_modules/jasmine-reporters/ext/jasmine.js and jasmine-html.js at the top of my <head>, and require all my specs in a top-level spec_entry.js that I then use as the entry point for a Browserify bundle that I put right afterwards in the <head>. (Note that if the entry point is not top-level, you'll have a bad time due to a long-lasting, gnarly bug in Browserify).

现在,我只在我的顶部包含/node_modules/jasmine-reporters/ext/jasmine.js和jasmine-html.js,并要求我在*spec_entry.js中使用我的所有规范作为我之后在中放置的Browserify包的入口点。 (请注意,如果入口点不是*,那么由于Browserify中存在持久且粗糙的错误,您将会遇到错误的时间)。

This plays nicely with jasmine-node as long as you don't assume the presence of a global document or window. However, you do have to remember to register your specs in that spec_entry.js, unless you want to hack Browserify to get it to crawl your directories for .spec.js files.

只要您不假设存在全局文档或窗口,这就可以很好地与jasmine-node一起使用。但是,您必须记住在spec_entry.js中注册您的规范,除非您想破解Browserify以使其抓取您的目录以获取.spec.js文件。

I'd be very interested in a more elegant solution, though, that would transparently work with jasmine-node and browserify.

不过,我对更优雅的解决方案非常感兴趣,它可以透明地与jasmine-node和browserify一起使用。

#2


8  

I've created a detailed example project which addresses the jasmine testing (and others) - see https://github.com/amitayd/grunt-browserify-jasmine-node-example. Discussion at my blog post

我已经创建了一个详细的示例项目来解决茉莉花测试(以及其他问题) - 请参阅https://github.com/amitayd/grunt-browserify-jasmine-node-example。在我的博客文章中讨论

The approach in this aspect was to create a Browserify bundle for the main source code (where all the modules are exposed), and one for tests which relies on external for the main source code. Then tests can be run both in PhantomJS or a real browser.

这方面的方法是为主要源代码(所有模块都公开)创建一个Browserify包,另一个用于主要源代码依赖外部的测试。然后可以在PhantomJS或真实浏览器中运行测试。

#3


4  

If you use grunt-watchify, no need to create spec_entry.js. Just use require in your specs, and then bundle your specs with grunt-watchify:

如果您使用grunt-watchify,则无需创建spec_entry.js。只需在您的规范中使用require,然后使用grunt-watchify捆绑您的规范:

    watchify: {
        test: {
            src: './spec/**/*Spec.js',
            dest: 'spec/spec-bundle.js'
        }
    },
    jasmine: {
        test: {
            options: {
                specs: 'spec/spec-bundle.js'
            }
        }
    },

Then run your tests with

然后运行您的测试

grunt.registerTask('test', ['watchify:test','jasmine:test']);

#4


1  

As all above answers are little outdated (of course it doesn't mean that they are not working any more etc.) I would like to point to https://github.com/nikku/karma-browserify this is a preprocessor for karma runner. It combines test files with all required dependencies. Such created browserify bundle is passed to karma which base on configuration runs it. Please be aware that you can choose any modern test framework (jasmin,mocha...) and browsers (phantom, chrome ..) Probably this is exactly what you need :)

由于以上所有答案都很过时(当然这并不意味着他们不再工作等等)我想指向https://github.com/nikku/karma-browserify这是业力的预处理器亚军。它将测试文件与所有必需的依赖项相结合。这样创建的browserify包被传递给karma,它基于配置运行它。请注意,你可以选择任何现代测试框架(jasmin,mocha ...)和浏览器(幻像,铬......)可能这正是你需要的:)

#5


0  

You may also want to look into Karma. It really simple to setup and it will watch for changes and rerun your test. Check out this sample project that uses Karma to test a browserify/react project. You just need to add a few dependancies and create a karma.conf.js file.

您可能还想看看Karma。设置非常简单,它会监视更改并重新运行测试。查看使用Karma测试browserify / react项目的示例项目。您只需添加一些依赖项并创建一个karma.conf.js文件。

https://github.com/TYRONEMICHAEL/react-component-boilerplate

https://github.com/TYRONEMICHAEL/react-component-boilerplate

#1


4  

I don't think there's a jasmine-browserify package yet, and it doesn't really match Browserify/NPM's way of doing things (avoid global exports).

我认为还没有一个jasmine-browserify软件包,它并不真正符合Browserify / NPM的做事方式(避免全球出口)。

For now, I just include /node_modules/jasmine-reporters/ext/jasmine.js and jasmine-html.js at the top of my <head>, and require all my specs in a top-level spec_entry.js that I then use as the entry point for a Browserify bundle that I put right afterwards in the <head>. (Note that if the entry point is not top-level, you'll have a bad time due to a long-lasting, gnarly bug in Browserify).

现在,我只在我的顶部包含/node_modules/jasmine-reporters/ext/jasmine.js和jasmine-html.js,并要求我在*spec_entry.js中使用我的所有规范作为我之后在中放置的Browserify包的入口点。 (请注意,如果入口点不是*,那么由于Browserify中存在持久且粗糙的错误,您将会遇到错误的时间)。

This plays nicely with jasmine-node as long as you don't assume the presence of a global document or window. However, you do have to remember to register your specs in that spec_entry.js, unless you want to hack Browserify to get it to crawl your directories for .spec.js files.

只要您不假设存在全局文档或窗口,这就可以很好地与jasmine-node一起使用。但是,您必须记住在spec_entry.js中注册您的规范,除非您想破解Browserify以使其抓取您的目录以获取.spec.js文件。

I'd be very interested in a more elegant solution, though, that would transparently work with jasmine-node and browserify.

不过,我对更优雅的解决方案非常感兴趣,它可以透明地与jasmine-node和browserify一起使用。

#2


8  

I've created a detailed example project which addresses the jasmine testing (and others) - see https://github.com/amitayd/grunt-browserify-jasmine-node-example. Discussion at my blog post

我已经创建了一个详细的示例项目来解决茉莉花测试(以及其他问题) - 请参阅https://github.com/amitayd/grunt-browserify-jasmine-node-example。在我的博客文章中讨论

The approach in this aspect was to create a Browserify bundle for the main source code (where all the modules are exposed), and one for tests which relies on external for the main source code. Then tests can be run both in PhantomJS or a real browser.

这方面的方法是为主要源代码(所有模块都公开)创建一个Browserify包,另一个用于主要源代码依赖外部的测试。然后可以在PhantomJS或真实浏览器中运行测试。

#3


4  

If you use grunt-watchify, no need to create spec_entry.js. Just use require in your specs, and then bundle your specs with grunt-watchify:

如果您使用grunt-watchify,则无需创建spec_entry.js。只需在您的规范中使用require,然后使用grunt-watchify捆绑您的规范:

    watchify: {
        test: {
            src: './spec/**/*Spec.js',
            dest: 'spec/spec-bundle.js'
        }
    },
    jasmine: {
        test: {
            options: {
                specs: 'spec/spec-bundle.js'
            }
        }
    },

Then run your tests with

然后运行您的测试

grunt.registerTask('test', ['watchify:test','jasmine:test']);

#4


1  

As all above answers are little outdated (of course it doesn't mean that they are not working any more etc.) I would like to point to https://github.com/nikku/karma-browserify this is a preprocessor for karma runner. It combines test files with all required dependencies. Such created browserify bundle is passed to karma which base on configuration runs it. Please be aware that you can choose any modern test framework (jasmin,mocha...) and browsers (phantom, chrome ..) Probably this is exactly what you need :)

由于以上所有答案都很过时(当然这并不意味着他们不再工作等等)我想指向https://github.com/nikku/karma-browserify这是业力的预处理器亚军。它将测试文件与所有必需的依赖项相结合。这样创建的browserify包被传递给karma,它基于配置运行它。请注意,你可以选择任何现代测试框架(jasmin,mocha ...)和浏览器(幻像,铬......)可能这正是你需要的:)

#5


0  

You may also want to look into Karma. It really simple to setup and it will watch for changes and rerun your test. Check out this sample project that uses Karma to test a browserify/react project. You just need to add a few dependancies and create a karma.conf.js file.

您可能还想看看Karma。设置非常简单,它会监视更改并重新运行测试。查看使用Karma测试browserify / react项目的示例项目。您只需添加一些依赖项并创建一个karma.conf.js文件。

https://github.com/TYRONEMICHAEL/react-component-boilerplate

https://github.com/TYRONEMICHAEL/react-component-boilerplate