Search code examples
restkarma-runnerpact

While running Pact- Karma-Mocha framework, getting error - 'Can't find variable: Pact'


I am trying to get sample PACT JS framework (any variant) running for Contract testing. Initial plan is just to get the sample(s) provided get running & then later on make changes into End point and customise for our own purpose.

PACT Foundation link: https://github.com/pact-foundation/pact-js/tree/master/karma/mocha Environment:

Win 7 Node: v8.11.4 dependencies installed: "@pact-foundation/karma-pact": { "version": "2.1.8", "@pact-foundation/pact-node": { "version": "6.19.11",

I am getting following error while trying to get it running.

enter image description here Command: karma start karma.conf.js

C:\VarProjects\VanillaMocha>karma start test/karma.conf.js

10 09 2018 09:53:34.544:ERROR [config]: File C:\VarProjects\VanillaMocha\test\karma.conf.js does not exist!

C:\VarProjects\VanillaMocha>karma start karma.conf.js [2018-09-10T08:53:42.384Z] INFO: pact-node@6.19.11/16892 on W5167037: Creating Pact Server with options: port = 1234, consumer = KarmaMochaConsumer, provider = KarmaMochaProvider, logLevel = DEBUG, log = C:\VarProjects\VanillaMocha\logs\pact.log, dir = C:\VarProjects\VanillaMocha\pacts, pactFileWriteMode = overwrite, ssl = false, cors = false, host = localhost [2018-09-10T08:53:42.401Z] INFO: pact-node@6.19.11/16892 on W5167037: Created 'standalone\win32-1.54.4\bin\pact-mock-service.bat service --port '1234' --consumer 'KarmaMochaConsumer ' --provider 'KarmaMochaProvider' --log-level 'DEBUG' --log 'C:\VarProjects\VanillaMocha\logs\pact.log' --pact_dir 'C:\VarProjects\VanillaMocha\pacts' --pact-file-write-mode 'overwri te' --host 'localhost'' process with PID: 18912 10 09 2018 09:53:44.980:INFO [pact]: Pact Mock Server running on port: 1234 10 09 2018 09:53:45.054:WARN [watcher]: Pattern "C:/dist-web/pact-web.js" does not match any file. 10 09 2018 09:53:45.092:INFO [karma]: Karma v3.0.0 server started at http://0.0.0.0:9876/ 10 09 2018 09:53:45.093:INFO [launcher]: Launching browser PhantomJS_without_security with unlimited concurrency 10 09 2018 09:53:45.101:INFO [launcher]: Starting browser PhantomJS 10 09 2018 09:53:46.811:INFO [PhantomJS 2.1.1 (Windows 7 0.0.0)]: Connected on socket qMhVUJZzdDCD_YuKAAAA with id 47921548 PhantomJS 2.1.1 (Windows 7 0.0.0) Client "before all" hook FAILED Can't find variable: Pact client-spec.js:10:32 PhantomJS 2.1.1 (Windows 7 0.0.0) Client "after all" hook FAILED undefined is not an object (evaluating 'provider.finalize') client-spec.js:21:28 PhantomJS 2.1.1 (Windows 7 0.0.0): Executed 2 of 4 (2 FAILED) ERROR (0.013 secs / 0.001 secs) [2018-09-10T08:53:46.985Z] INFO: pact-node@6.19.11/16892 on W5167037: Removing all Pact servers. [2018-09-10T08:53:46.986Z] INFO: pact-node@6.19.11/16892 on W5167037: Removing Pact with PID: 18912

C:\VarProjects\VanillaMocha>KARMA start [2018-09-10T08:54:14.809Z] INFO: pact-node@6.19.11/7492 on W5167037: Creating Pact Server with options: port = 1234, consumer = KarmaMochaConsumer, provider = KarmaMochaProvider, logLevel = DEBUG, log = C:\VarProjects\VanillaMocha\logs\pact.log, dir = C:\VarProjects\VanillaMocha\pacts, pactFileWriteMode = overwrite, ssl = false, cors = false, host = localhost [2018-09-10T08:54:14.823Z] INFO: pact-node@6.19.11/7492 on W5167037: Created 'standalone\win32-1.54.4\bin\pact-mock-service.bat service --port '1234' --consumer 'KarmaMochaConsumer' --provider 'KarmaMochaProvider' --log-level 'DEBUG' --log 'C:\VarProjects\VanillaMocha\logs\pact.log' --pact_dir 'C:\VarProjects\VanillaMocha\pacts' --pact-file-write-mode 'overwrit e' --host 'localhost'' process with PID: 2920 10 09 2018 09:54:17.376:INFO [pact]: Pact Mock Server running on port: 1234 10 09 2018 09:54:17.447:WARN [watcher]: Pattern "C:/dist-web/pact-web.js" does not match any file. 10 09 2018 09:54:17.483:INFO [karma]: Karma v3.0.0 server started at http://0.0.0.0:9876/ 10 09 2018 09:54:17.484:INFO [launcher]: Launching browser PhantomJS_without_security with unlimited concurrency 10 09 2018 09:54:17.489:INFO [launcher]: Starting browser PhantomJS 10 09 2018 09:54:19.243:INFO [PhantomJS 2.1.1 (Windows 7 0.0.0)]: Connected on socket rn-kwBRGhJbyUwvZAAAA with id 54614606 PhantomJS 2.1.1 (Windows 7 0.0.0) Client "before all" hook FAILED Can't find variable: Pact client-spec.js:10:32 PhantomJS 2.1.1 (Windows 7 0.0.0) Client "after all" hook FAILED undefined is not an object (evaluating 'provider.finalize') client-spec.js:21:28 PhantomJS 2.1.1 (Windows 7 0.0.0): Executed 2 of 4 (2 FAILED) ERROR (0.012 secs / 0 secs)

Kindly advise, Thanks a lot


Solution

  • The code is failing because it's missing pact-web. You can see this in the error message you included:

    Pattern "C:/dist-web/pact-web.js" does not match any file. 
    

    This is happening because you're using the example karma.conf.js outside the example repository without modification.

    Quoting the relevant part of karma.conf.js:

    // if you are using this example to setup your own project 
    // load pact from the node_modules directory
    '../../dist-web/pact-web.js',
    // Example Using NPM package
    // 'node_modules/@pact-foundation/pact-web/pact-web.js',
    

    Looking at your directory structure, commenting this line:

    '../../dist-web/pact-web.js',
    

    and uncommenting this line:

    // 'node_modules/@pact-foundation/pact-web/pact-web.js',
    

    should solve your problem.

    Note that you also need to ensure that @pact-foundation/pact-web is a dev-dependency:

    npm install --save-dev '@pact-foundation/pact-web'