I can't understand how to use the createDummyGenerator() function when testing a generator that relies on external generators.
I have tried:
test.js:
...
return helpers.run(require.resolve('../generators/app'))
.withGenerators([
[helpers.createDummyGenerator(), 'license:app'],
])
.then(() => {
assert.textEqual('true', 'true')
});
...
index.js:
...
default() {
this.composeWith('license:app', { name: 'foo' });
}
...
This makes the test fail because it can't find a generator for license:app. I have generator-license in my package.json as a dependency.
I also tried the following:
test.js:
...
beforeEach(() => {
jest.mock('generator-license/app', () => {
const helpers = require('yeoman-test');
return helpers.createDummyGenerator();
});
}
...
index.js:
...
default() {
this.composeWith(require.resolve('generator-license/app', { name: 'foo' }));
}
...
This doesn't mock the generator at all, and it uses the actual generator-license code, which makes the test fail because not all prompts are supplied (some are meant to be asked by the license generator)
How am I supposed to use the createDummyGenerator() helper to completely stub out the license generator?
Well, I feel like an idiot... I had a typo in another test that didn't mock the module, and that's what was making the test suite fail... Nevermind, nothing to see here :)
Related
I want module sync-fetch to be accessible globally without need to import in each component and be named as simple fetch.
Also I want to extend it with custom method then.
Now in rollup.config.js there are:
export default {
...
output: {
...
intro: `const fetch = require('sync-fetch');
fetch.Response.prototype.then = function(foo) {
return foo(this);
}`
},
};
And it works, but looks dangerous) Is intro is the only way to do it?
If you want to make it seem less dangerous, you could put that code in a file and then return the contents of it in a function. The output.intro option also takes a function that returns the code as a string.
{
output: {
intro: () => require('fs/promises').readFile('path/to/the/file.js', 'utf-8')
}
}
I was using XMLSerializer class in one of NODE package class method. When writing unit test case using jest it threw error of XMLSerializer is not undefined. As this is native to browser, there can not be any package.
Even I searched online, found few suggestions of using a new jsdom and creating a prototype of XMLSerializer.
No proper solution is found even no posts at SO.
I've currently used a wrapped for XMLSerializer as below (typescript code):
export default class XmlSerializerWrapper {
private serializer: XMLSerializer;
constructor() {
this.serializer = new XMLSerializer();
}
public serializeToString(doc: Document) {
return this.serializer.serializeToString(doc);
}
}
It works fine.
Had the same issue today and came upon this post. My approach:
describe('MyXmlDocument', () => {
global.XMLSerializer = function() {
return {
serializeToString : jest.fn(x => x)
};
}
... your tests here
});
My code only needs the serializeToString() method, so that's the only one mocked.
I have created vue components using this structure.
Vue.component('my-component', {
template: '<div>A custom component!</div>'
})
I would like to test this using karma and jasmine or jasmine rails gem. I can't figure out how to test the component. In all the examples on the docs they use a requirejs module way of testing. I use the global component way of creating components.
These are the examples from the docs.
<template>
<span>{{ message }}</span>
</template>
<script>
export default {
data () {
return {
message: 'hello!'
}
},
created () {
this.message = 'bye!'
}
}
</script>
// Import Vue and the component being tested
import Vue from 'vue'
import MyComponent from 'path/to/MyComponent.vue'
// Here are some Jasmine 2.0 tests, though you can
// use any test runner / assertion library combo you prefer
describe('MyComponent', () => {
// Inspect the raw component options
it('has a created hook', () => {
expect(typeof MyComponent.created).toBe('function')
})
// Evaluate the results of functions in
// the raw component options
it('sets the correct default data', () => {
expect(typeof MyComponent.data).toBe('function')
const defaultData = MyComponent.data()
expect(defaultData.message).toBe('hello!')
})
// Inspect the component instance on mount
it('correctly sets the message when created', () => {
const vm = new Vue(MyComponent).$mount()
expect(vm.message).toBe('bye!')
})
// Mount an instance and inspect the render output
it('renders the correct message', () => {
const Ctor = Vue.extend(MyComponent)
const vm = new Ctor().$mount()
expect(vm.$el.textContent).toBe('bye!')
})
})
import foo from 'bar'; and var foo = require('bar'); does the same thing, making foo available as a variable in the current file. In the test example, MyComponent is the imported vue component instance, which can also be achieved with MyComponent = Vue.component(...); in the current file. So if your test is in the same file, just use the MyComponent variable, if not, you'll need to export MyComponent first with module.exports = MyComponent or export default MyComponent. These exports assume MyComponent is the only thing you want to export, if you want to export multiple variables, checkout the docs: http://wiki.commonjs.org/wiki/Modules/1.1 https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/export
My app is accessing object from the service scope (package:gcloud/service_scope.dart), like the storageService and additional services that I put inside the scope with ss.register().
Now I want to unit test a function that accesses this scope, and uses mock objects that I want to put in the service scope.
Is the only way to do so, to register them for every test, like this:
var withServiceScope = (callback()) => ss.fork(() {
// Register all services here
return callback();
});
test('the description', () => withServiceScope(() async {
// Call my function that can now access the service scope
}));
Or is there are way that allows me to do that in the setUp() function so I don't need to add this line for each test?
This might make it simpler to write your tests (code not tested)
import 'package:test/test.dart' as t;
import 'package:test/test.dart' show group;
var withServiceScope = (callback()) => ss.fork(() {
// Register all services here
return callback();
});
test(String description, Function testFunction) {
t.test(description, () => withServiceScope(() async {
testFunction();
}));
}
main() {
test('the description', () async {
// Call my function that can now access the service scope
}));
}
I'm trying to make a generator that I can then extend to form similar sub-generators
Here's my base generator:
var generators = require('yeoman-generator');
var VolumeAdderBase = module.exports = generators.Base.extend({
initializing: function() {
/* ... */
},
prompting: function () {
/* ... */
},
writing: function () {
/* ... */
}
});
Here's an example sub-generator:
var VolumeAdderBase = require('../../utils/VolumeAdderBase.js');
// console.log(VolumeAdderBase);
module.exports = VolumeAdderBase.extend({
fileType: "tomcat script",
containerName: "tomcat",
containerVolumeLocation: "/opt/tomcat/client-conf/"
});
When I try to run my sub-generator, it does nothing at all. No errors, no nothing.
When I dump the VolumeAdderBase object, there are plenty of methods on there, but they are all the Base ones. The ones defined in VolumeAdderBase are missing.
Am I missing something here? Or is there a better way to create similar sub-generators?
yeoman-generator is only going to run top level methods (Object.getOwnPropertyNames(prototype)). It doesn't go deeper in the prototype; that's by design. If Yeoman was to dig in the prototype, you couldn't use methods like this.destinationPath() or any other helpers as they'd all be schedule to be run - which would just break.
We have plans to support mixin in the future. But that's not currently the case.
As a fix, you can manually assign these methods:
VolumeAdderBase.prototype.prompting = VolumeAdderBase.prototype.prompting;
// etc...