Error: Cannot find module - although it is imported in the first line

I’m trying to do a acceptance-test, but I have to use Meteor.userId(). So I thought I should use sinon and stub. But I do get the error Error: Cannot find module 'meteor/practicalmeteor:sinon', which I do not understand, as I’m importing this:

import { sinon } from 'meteor/practicalmeteor:sinon';

describe('Chimp Mocha', () => {
	let sandbox;

	beforeEach(function() {
		sandbox = sinon.sandbox.create();
	});

	afterEach(function() {
		sandbox.restore();
	});
	describe('logged user', () => {
		it('should get some data @watch', () => {
			sandbox.stub(Meteor, 'userId').returns('42');
		});
	});
});

Did you also do

meteor add practicalmeteor:sinon

?

Yes, I did that.

And I start the test with chimp --ddp=http://localhost:3000 --mocha --path=tests --watch.

Is this code inside a local meteor package? If so, you’ll need to api.use() the sinon package in your package.js

no, it is inside of a file, which is stored in the tests-folder of meteor

Chimp.js runs outside of the Meteor ecosystem. I’m pretty sure that means it can’t use Meteor packages directly (which means it can’t use meteor/X based imports, since these are interpreted and handled by the Meteor build tool). You might want to look into using sinon directly from npm instead.

So you mean import {spy, stub} from 'sinon';?
This gives me Error: Cannot find module 'sinon'

To use sinon from npm with Meteor, you would have to npm install it first:

meteor npm install --save-dev sinon

That being said, the process with Chimp is likely different (since again Chimp runs outside of Meteor). You’ll likely have to set this up a bit differently (well, not that differently since you’ll still need to npm install sinon somewhere and have it picked up by Chimp). I’m not overly familiar with Chimp, so maybe @sam can help.

Ah, I see… Thanks for your help. Now I understand it a little better.

as @hwillson has alluded, Chimp runs outside the Meteor context. It’s actually just a node application that combines a set of tools to give you easy end-to-end testing capability.

So when stubbing / calling Meteor specific things, you can’t do those directly from Chimp (though you can use server.execute(fn) to run arbitrary code on Meteor from within a Chimp test).

Hope that helps

1 Like

@sam yes, that helps, but I got a little problem with server.execute().

const 	docId = browser.getAttribute('#target', 'data-id'),
result = server.execute(() => { return Collection.find({ _id: docId }).fetch(); });

Why do I get Error: Error in server.executeReferenceError: docId is not defined? Although docId has definitely a value.

try

const docIdFromChimpContext = browser.getAttribute('#target', 'data-id');

const result = server.execute((docIdInMeteorContext) => { 
  return Collection.find({ _id: docIdInMeteorContext }).fetch();
}, docIdFromChimpContext);