Posted 21 days ago. Visible to the public. Linked content. Auto-destruct in 39 days
Updated: Jasmine: Fixing common errors during initialization
Updated esbuild patch for Jasmine 4.3 to use let
instead of var
(because that is what Jasmine's code now uses).
Changes
- Due to the [way we setup Jasmine tests](https://makandracards.com/makandra/75556-adding-jasmine-javascript-specs-to-a-webpack-er-project) in our projects, you may run into various errors when Jasmine boots.
- # Setting `jasmineRequire` on undefined
- Jasmine 4 may fail with an error like this:
- ```text
- Uncaught TypeError: Cannot set properties of undefined (setting 'jasmineRequire')
- ```
- This is due to issues in Jasmine's [environment detection](https://github.com/jasmine/jasmine/blob/502cb24bb89212917a3c943b593fd918ffc481cb/lib/jasmine-core/jasmine.js#L28-L47). It must be fixed by patching the Jasmine sources.
- ## Fix for Webpacker
- Add the package `string-replace-loader` to your `package.json`. If you're on Webpacker 5 (Webpack 4) the latest version you can use is `2.x`.
- Now add the following loader to your `config/webpack/environment.js`:
- ```js
- environment.loaders.prepend('fix-jasmine4-global-detection', {
- test: /jasmine-core\/lib\/jasmine-core\/jasmine\.js$/,
- use: [{
- loader: 'string-replace-loader',
- options: {
- search: 'window.toString() === \'[object GjsGlobal]\'',
- replace: 'window.toString() === \'[object Window]\''
- }
- }]
- })
- ```
- ## Fix for ESBuild / jsbundling-rails
- Add the package `esbuild-plugin-text-replace` to your `package.json`.
- Now add the following plugin to your `esbuild.config.js`:
- ```js
- const textReplace = require('esbuild-plugin-text-replace')
- const esbuild = require('esbuild')
- esbuild.build({
- ...
- plugins: [
- textReplace({
- include: /jasmine-core\/lib\/jasmine-core\/jasmine\.js$/,
- pattern: [
- ['var jasmineRequire;', 'var jasmineRequire; const global = window;'],- + ['let jasmineRequire;', 'let jasmineRequire; const global = window;'],
- ],
- }),
- ],
- })
- ```
- +If you are on Jasmine < 4.3.0, use `var` instead of `let` in the code above.
- +
- # Missing `getJasmineRequireObj()`
- If you get this error:
- ```text
- Uncaught ReferenceError: getJasmineRequireObj is not defined
- ```
- then you need a manually expose `getJasmineRequireObj()` to your specs.
- ## Fix for Webpack
- Add a file
- ```js
- // app/webpack/spec/support/jasmine_provider.js
- import jasmineRequire from 'jasmine-core/lib/jasmine-core/jasmine.js'
- export default function getJasmineRequireObj() {
- return jasmineRequire;
- }
- ```
- Then expose the provider in your `config/webpack/environment.js`:
- ```js
- const webpack = require('webpack')
- environment.plugins.prepend('Provide', new webpack.ProvidePlugin({
- getJasmineRequireObj: ['spec/support/jasmine_provider.js', 'default'],
- }))
- ```
- ## Fix for ESBuild / jsbundling-rails
- TODO.
Does your version of Ruby on Rails still receive security updates?
Rails LTS provides security patches for unsupported versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2).