Search code examples
requirejspact

Karma/jasmine/pact-js example using requirejs


I am working on the karma/jasmine example. It works fine as-is. I need to get it to work with requirejs. When I introduce requirejs and convert the client and client-spec classes to AMDs, I start getting this error:

PhantomJS 2.1.1 (Windows 7 0.0.0) Client sayHello should say hello FAILED TypeError: undefined is not a constructor (evaluating 'Pact') in js/client-spec.js (line 11) js/client-spec.js:11:28

To see the problem, you can look at my example project. To run it:

  1. Extract the zip file in my example project above
  2. From the PactTest directory, run: npm install
  3. After that is finished, run: npm run test:client

Cheers, Mike


Solution

  • You need to set the module name to "Pact" instead of "pact-web". It seems that requirejs can't find the matching module name.

    In client-spec.js change the define to:

    define(['client', 'Pact'], function (example, Pact) {
    

    In test-main.js update the config as follows:

    require.config({
        baseUrl: '/base',
        paths: {
            // 'pact-web': 'node_modules/pact-web/pact-web', <-- not this
            'Pact': 'node_modules/pact-web/pact-web',
            'client': 'js/client'
        },
        deps: allTestFiles,
        callback: window.__karma__.start
    })
    

    Admittedly, I don't fully understand the ins and outs of RequireJS, but this does work.

    I've updated the Pact documentation to include a note on this.

    Here is a link to an updated Gist for future reference: https://gist.github.com/mefellows/15c9fcb052c2aa9d8951f91d48d6da54