jasmine / jasmine-browser-runner

Serve and run your Jasmine specs in a browser
49 stars 23 forks source link

[question] Can I inject library code into the test runner? #7

Closed dotnetCarpenter closed 2 years ago

dotnetCarpenter commented 3 years ago

I'm trying to add jasmine to a legacy project but I have not been able to find if it's possible or how to inject library code into the test runner page.

I see that each file in my srcDir is included on the page served on localhost:8888. Our client codebase expects underscore.js and jQuery but where do I define them as globally available when we do not have a package system or use ESM?

I would like to use jasmine for our nodejs express code as well so using the Standalone version would probably mean that we will have to maintain two versions of Jasmine until we have time to rewrite our project (which could benefit hugely from having unit-tests in that process).

The beginning of the test page:

<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html;charset=UTF-8" http-equiv="Content-Type"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Jasmine suite</title>
<link rel="shortcut icon" type="image/png" href="/__images__/jasmine_favicon.png">
 
<link rel="stylesheet" href="/__jasmine__/jasmine.css" type="text/css" media="screen"/>
 
 
<script src="/__jasmine__/jasmine.js" type="text/javascript"></script>
 
<script src="/__jasmine__/jasmine-html.js" type="text/javascript"></script>
 
<script src="/__jasmine__/json2.js" type="text/javascript"></script>
 
<script src="/__boot__/boot.js" type="text/javascript"></script>
 
<script src="/__support__/loadEsModule.js" type="text/javascript"></script>
 
 
<!-- Start: import of my code  -->
<script src="/__src__/blocks/f-progress-stretch.js" type="text/javascript"></script>
<!-- ect... -->

jasmine-browser.json

{
  "srcDir": "public/js",
  "srcFiles": [
    "**/*.?(m)js"
  ],
  "specDir": "spec",
  "specFiles": [
    "**/*[sS]pec.?(m)js"
  ],
  "helpers": [
    "helpers/**/*.?(m)js"
  ],
  "random": true,
  "env": {},
  "browser": {
    "name": "firefox"
  }
}

nodejs: v14.17.3 jasmine: 3.8.0 jasmine-browser-runner: 0.7.0

sgravrock commented 3 years ago

I assume that your actual app includes the libraries via script tags. If that's the case you should be able to add them to the srcFiles list. For instance, if jQuery is public/libs/jQuery.js:

{
  "srcDir": "public/js",
  "srcFiles": [
    "../libs/jQuery.js",
    "**/*.?(m)js"
  ],
// [...]
dotnetCarpenter commented 3 years ago

I currently use jsDelivr CDN. https://cdn.jsdelivr.net/npm/....

Using Karma I can add external files like so:

// List of files/patterns to load in the browser.
files: [
  'https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js',
  'https://cdn.jsdelivr.net/npm/underscore@1.13.1/underscore-min.js',
{
  pattern: 'public/js/*',
  included: true,
  served: true,
  watched: false,
  nocache: false,
  type: 'js'
},{
  pattern: 'spec/unit/*.js',
  included: true,
  served: true,
  watched: false,
  nocache: false,
  type: 'js'
}],

jasmine-browser-runner looks interesting but I needed to get it working ASAP, so I switched to Karma.

Leaving this open, in case you want to implement something similar to Karma's solution, but feel free to close this.

Cheers, and thank you for the attention :)

sgravrock commented 3 years ago

That seems reasonable. (It's always surprised me that almost nobody in the long history of Jasmine's other runners has asked for it.) I think the easiset and likely best way to implement it would be to allow any of the file glob arrays (srcFiles, specFiles, and helpers) to contain URLs as well as file globs. I'd be happy to review a PR that added that functionality.

dotnetCarpenter commented 3 years ago

Just to clarify. I will not work on this issue, since I use Karma now (which has support for CDN).

But I'm interested and will keep an eye out for this space.

sgravrock commented 2 years ago

This is done in 027690a and will be in the next release.