Git Product home page Git Product logo

Comments (26)

ChrisGibb avatar ChrisGibb commented on September 17, 2024 4

I had the same problem, not with pact, but Ionic + Angular + Karma. The problem for me was caused by Webstorm sticking a bad import in one of my spec files:

import { describe } from "selenium-webdriver/testing";

Removed it and all got back to normal

from pact-js.

NomiJ avatar NomiJ commented on September 17, 2024 1

Hey @mefellows ,
Thanks, I will give it a go and let you know soon.

from pact-js.

mefellows avatar mefellows commented on September 17, 2024

Hi @NomiJ, I'm not familiar with Angular2 testing practices, but based on the log file you've provided it seems to use Karma under the hood. Have you looked at the karma examples for any hints?

In any case, child_process is only available in a proper Node process, not any JS execution environment (such as a browser). You will need to start the mock service independently of your test suite for this to work. Typically, this is orchestrated via bash/grunt/gulp/.

If you could provide a working example to repro that'd be even better.

from pact-js.

mboudreau avatar mboudreau commented on September 17, 2024

@NomiJ to further what @mefellows is saying, Pact should never be run in the browser and needs to be run in a node environment. If you are doing unit tests with angular, you'll need something to run pact-node first (could be done with the build tool or npm scripts), then use pact-js to communicate the interactions with it. It could also be done using protractor if you want to do end to end tests since protractor runs fully in node.

from pact-js.

mefellows avatar mefellows commented on September 17, 2024

@NomiJ have you got any update on this?

from pact-js.

NomiJ avatar NomiJ commented on September 17, 2024

Hi,
Sorry for late reply. Explanation make sense. Due to shortage of time I ended up moving to Depreciated DSL here (https://github.com/DiUS/pact-consumer-js-dsl), yet would definitely like to upgrade to maintained package. I guess, i can always spawn the server as a separate process or gulp task. I will be creating a example soon and share it with you.

from pact-js.

NomiJ avatar NomiJ commented on September 17, 2024

I think i am missing something basic. Here I have created a basic angular2 App created with Angular-Cli (Release 1.0). I added @pact-foundation/pact-js and pact dependencies and created a basic "SayHello" test scenario.

https://github.com/NomiJ/Pact-Angular2-Example/blob/master/src/app/animal.service.spec.ts

Now running ng test outputs this
`
WARNING in .//source-map-support/source-map-support.js
Module not found: Error: Can't resolve 'module' in '/Users/nomi/Desktop/curveTomorrow/Pact-Angular2-Example/node_modules/source-map-support'
@ ./
/source-map-support/source-map-support.js 474:15-32
@ .//bunyan/lib/bunyan.js
@ ./
/@pact-foundation/pact-node/src/logger.js
@ .//@pact-foundation/pact-node/src/pact.js
@ ./
/pact/src/pact.js
@ ./src/app/animal.service.spec.ts
@ ./src .spec.ts$
@ ./src/test.ts

WARNING in ./~/pact/src/common/config.js
21:19-44 Critical dependency: the request of a dependency is an expression

WARNING in ./~/@pact-foundation/pact-node/src/publisher.js
163:12-24 Critical dependency: the request of a dependency is an expression

WARNING in ./~/@pact-foundation/pact-mock-service/index.js
7:17-37 Critical dependency: the request of a dependency is an expression

WARNING in ./~/@pact-foundation/pact-provider-verifier/index.js
7:17-37 Critical dependency: the request of a dependency is an expression

ERROR in .//@pact-foundation/pact-node/src/server.js
Module not found: Error: Can't resolve 'child_process' in '/Users/nomi/Desktop/curveTomorrow/Pact-Angular2-Example/node_modules/@pact-foundation/pact-node/src'
@ ./
/@pact-foundation/pact-node/src/server.js 8:6-30
@ .//@pact-foundation/pact-node/src/pact.js
@ ./
/pact/src/pact.js
@ ./src/app/animal.service.spec.ts
@ ./src .spec.ts$
@ ./src/test.ts

ERROR in .//@pact-foundation/pact-node/src/verifier.js
Module not found: Error: Can't resolve 'child_process' in '/Users/nomi/Desktop/curveTomorrow/Pact-Angular2-Example/node_modules/@pact-foundation/pact-node/src'
@ ./
/@pact-foundation/pact-node/src/verifier.js 6:6-30
@ .//@pact-foundation/pact-node/src/pact.js
@ ./
/pact/src/pact.js
@ ./src/app/animal.service.spec.ts
@ ./src .spec.ts$
@ ./src/test.ts
28 02 2017 22:42:02.590:ERROR [karma]: { Error
at MemoryFileSystem.readFileSync (/Users/nomi/Desktop/curveTomorrow/Pact-Angular2-Example/node_modules/memory-fs/lib/MemoryFileSystem.js:107:10)
at MemoryFileSystem.(anonymous function) [as readFile] (/Users/nomi/Desktop/curveTomorrow/Pact-Angular2-Example/node_modules/memory-fs/lib/MemoryFileSystem.js:300:34)
at doRead (/Users/nomi/Desktop/curveTomorrow/Pact-Angular2-Example/node_modules/karma-webpack/lib/karma-webpack.js:205:29)
at Plugin.readFile (/Users/nomi/Desktop/curveTomorrow/Pact-Angular2-Example/node_modules/karma-webpack/lib/karma-webpack.js:210:7)
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)
code: 'ENOENT',
errno: 34,
message: 'no such file or directory',
path: '/karma_webpack/src/polyfills.ts' }
28 02 2017 22:42:02.607:ERROR [karma]: { Error
at MemoryFileSystem.readFileSync (/Users/nomi/Desktop/curveTomorrow/Pact-Angular2-Example/node_modules/memory-fs/lib/MemoryFileSystem.js:107:10)
at MemoryFileSystem.(anonymous function) [as readFile] (/Users/nomi/Desktop/curveTomorrow/Pact-Angular2-Example/node_modules/memory-fs/lib/MemoryFileSystem.js:300:34)
at doRead (/Users/nomi/Desktop/curveTomorrow/Pact-Angular2-Example/node_modules/karma-webpack/lib/karma-webpack.js:205:29)
at Plugin.readFile (/Users/nomi/Desktop/curveTomorrow/Pact-Angular2-Example/node_modules/karma-webpack/lib/karma-webpack.js:210:7)
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)
code: 'ENOENT',
errno: 34,
message: 'no such file or directory',
path: '/karma_webpack/src/test.ts' }`

My Environment:

@angular/cli: 1.0.0-rc.0 node: 7.0.0 os: darwin x64 @angular/common: 2.4.8 @angular/compiler: 2.4.8 @angular/core: 2.4.8 @angular/forms: 2.4.8 @angular/http: 2.4.8 @angular/platform-browser: 2.4.8 @angular/platform-browser-dynamic: 2.4.8 @angular/router: 3.4.8 @angular/cli: 1.0.0-rc.0 @angular/compiler-cli: 2.4.8 npm: 4.0.1

If i uninstall "@pact-foundation/pact-node", I get pact complaining like
ERROR in ./~/pact/src/pact.js Module not found: Error: Can't resolve '@pact-foundation/pact-node' in '/Users/nomi/Desktop/curveTomorrow/Pact-Angular2-Example/node_modules/pact/src' @ ./~/pact/src/pact.js 16:23-60 @ ./src/app/animal.service.spec.ts @ ./src \.spec\.ts$ @ ./src/test.ts

from pact-js.

NomiJ avatar NomiJ commented on September 17, 2024

Any update?

from pact-js.

mefellows avatar mefellows commented on September 17, 2024

OK part of the problem here is that both the consumer and the provider side of Pact JS must have access to a runtime environment outside of the browser (it needs to be able to spin up web servers and command-line utilities) - this use case is similar to Karma.

Take a look at the karma test for an example of how to do this, but for now I don't think it is possible to do what you want to do with the pact npm package.

That being said, the code exists and is published as a release. You could certainly pull this down, and use the pact-karma package for this. I'll raise an issue to rename it because AFAIK there is nothing Karma specific in it.

(You might be able to do something dodgey like const PactWeb = require('pact/dist/pact.web.js'); but this is getting into juggling chainsaw territory - do it at your own risk).

from pact-js.

 avatar commented on September 17, 2024

I am in the same boat, any help is appreciated

from pact-js.

mefellows avatar mefellows commented on September 17, 2024

Hi there,

I've just pushed up a test branch https://github.com/pact-foundation/pact-js/tree/feature/pact-web that avoids pulling in the Pact Node library - the cause of all of the above errors.

Could you please test out pointing to this GH branch instead of the published package to see if this addresses your issues?

You'll need to do something like the following to your package.json:

"devDependencies": {
   "pact": "pact-foundation/pact-js#feature\/pact-web",
}

And when you create your Pact object:

    provider = Pact({
      consumer: 'Karma Jasmine',
      provider: 'Hello',
      web: true // <- this is the bit that matters
    });

The bits that use pact-node are now wrapped inside functions that shouldn't be called by your code, I'm hoping that dead code elimination from tools like Webpack, Browserify or the Angular tooling should prune these imports. Else, we'll need a separate module release for this purpose.

from pact-js.

NomiJ avatar NomiJ commented on September 17, 2024

sure, i will test and report back

from pact-js.

mefellows avatar mefellows commented on September 17, 2024

from pact-js.

NomiJ avatar NomiJ commented on September 17, 2024

do i need keep this?
>> "@pact-foundation/pact-node": "4.8.0",
Thats how the current package.json looks like
"devDependencies": { "@angular/cli": "1.0.0-rc.0", "@angular/compiler-cli": "^2.4.0", "@pact-foundation/pact-node": "4.8.0", "@types/jasmine": "2.5.38", "@types/node": "~6.0.60", "codelyzer": "~2.0.0", "jasmine-core": "~2.5.2", "jasmine-spec-reporter": "~3.2.0", "karma": "~1.4.1", "karma-chrome-launcher": "~2.0.0", "karma-cli": "~1.0.1", "karma-coverage-istanbul-reporter": "^0.2.0", "karma-jasmine": "~1.1.0", "karma-jasmine-html-reporter": "^0.2.2", "pact": "pact-foundation/pact-js#feature\/pact-web", "protractor": "~5.1.0", "ts-node": "~2.0.0", "tslint": "~4.4.2", "typescript": "~2.0.0" }

from pact-js.

mefellows avatar mefellows commented on September 17, 2024

from pact-js.

NomiJ avatar NomiJ commented on September 17, 2024

Probably its a must a to run the mock server for tests to execute correctly, yet, i did not do it, considering the error
`
ERROR in .//@pact-foundation/pact-node/src/server.js
Module not found: Error: Can't resolve 'child_process' in '/private/tmp/Pact-Angular2-Example/node_modules/@pact-foundation/pact-node/src'
@ ./
/@pact-foundation/pact-node/src/server.js 8:6-30
@ .//@pact-foundation/pact-node/src/pact.js
@ ./
/pact/src/pact.js
@ ./src/app/animal.service.spec.ts
@ ./src .spec.ts$
@ ./src/test.ts

ERROR in .//@pact-foundation/pact-node/src/verifier.js
Module not found: Error: Can't resolve 'child_process' in '/private/tmp/Pact-Angular2-Example/node_modules/@pact-foundation/pact-node/src'
@ ./
/@pact-foundation/pact-node/src/verifier.js 6:6-30
@ .//@pact-foundation/pact-node/src/pact.js
@ ./
/pact/src/pact.js
@ ./src/app/animal.service.spec.ts
@ ./src .spec.ts$
@ ./src/test.ts`

Just to be sure, after npm install, i verified existence of your recent changes in pact.js, (addition of web ).

from pact-js.

NomiJ avatar NomiJ commented on September 17, 2024

What is pact-karma.js is about? Since Angular2 uses karma in my template. I checked your sample for karma-jasmine, yet it seems to load file from dist, in karma.conf.js, which probably require running npm dist, to load pact.web.js from it. Is that how it suppose to be?
Would really appreciate a Typescript or angular2 sample from pact team.

from pact-js.

mefellows avatar mefellows commented on September 17, 2024

Pact Karma is a module that will automatically start a Mock Server for Karma tests. You can pull that in with the npm package karma-pact.

The pact-karma.js file is the way to run Pact in a Browser (or non-Node environment). I'm thinking that it might make more sense to rename this, since there's no reason why it can't be used in non-Karma environments.

yet it seems to load file from dist, in karma.conf.js, which probably require runningnpm dist, to load pact.web.js from it. Is that how it suppose to be?

You could, although it has a release for the DSL. I'm not sure why this isn't released as an npm or bower style package, I'm following this up separately.

The Pact Karma plugin which manages the Mock Server has an npm package that you can pull down.

This is the ideal way to approach this.

from pact-js.

mefellows avatar mefellows commented on September 17, 2024

OK, just had a chat with the original author of the Karma stuff. I'm looking into how we can release that thing separately. For now, you'll need to find a manual way of pulling down that pact.web.js file.

Sorry about the confusion!

from pact-js.

mefellows avatar mefellows commented on September 17, 2024

Hi @NomiJ, @jimishah , I have renamed the karma package to pact-web and published a test package to https://www.npmjs.com/package/pact-web.

Would either of you care to give this a try?

from pact-js.

koelec avatar koelec commented on September 17, 2024

Hey @mefellows,
Thanxx
Got this working with the following package.json:
{
..
"karma-pact": "0.0.7",
"karma-phantomjs-launcher": "~1.0.4",
"pact": "^2.5.0",
"pact-web": "^2.5.0",
..
}
In my client.spec.ts I use:
let Pact = require('pact-web')
let provider = Pact({ consumer: 'Karma Jasmine', provider: 'Hello', web: true })

from pact-js.

mefellows avatar mefellows commented on September 17, 2024

Excellent!

from pact-js.

NomiJ avatar NomiJ commented on September 17, 2024

Also works for me. Anyone looking for example can look at

https://github.com/NomiJ/Pact-Angular2-Example

from pact-js.

mefellows avatar mefellows commented on September 17, 2024

from pact-js.

carlosmmelo avatar carlosmmelo commented on September 17, 2024

Hey @NomiJ this is not working for me. I'm using angular2 and Karma+Jasmine. is this outdated?

from pact-js.

NomiJ avatar NomiJ commented on September 17, 2024

I did not update the example, afterwards. I am sorry, its not working for you. Whats the problem that you are facing ?

from pact-js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.