Unit testing your plugins


#1

So I have been working on adding unit testing to my Jovo V2 skill the last couple weeks. Today I switched gears and added the Slack Error reporting plugin and got it working, which is very nice!

Now I am wondering how I could add some unit testing for this plugin, as well as I have a dashbot plugin installed that have no actual unit tests.

Anyone else out there have tests setup for these nifty plugins or ideas on how that would work?

To test while I added the Slack plugin, I added a error intent that I could call saying “I am error” and just returned a tell with no speech. However, I cannot keep that in and still get certified, so not a good long term plan.


#2

Hi @natrixx, thank you, this is an interesting question.

Just to clarify: You want to add unit tests for a plugin to your application rather than add unit tests to a plugin itself?

Maybe @AlexSwe, @rubenaeg, or @Kaan_Kilic can help, who have most experience with writing unit tests for the framework.


#3

@jan - that is correct, I would like to test my implementation of the plugin rather than the plugin itself.

Having thought it about it some more since asking, I suspect being able to mock some stuff would come in handy here, as mentioned in this link: Feature Proposal: Same-process testing


#4

Hey Nate,

this feature was implemented by @Stephen_Wilcox already. I haven’t tested it with mocks though.
Here’s the sample code:
https://github.com/jovotech/jovo-framework/blob/38e918f57317616d24c99764aeae10ae87bf94fe/examples/javascript/unit-testing/test/sample_sendToApp.test.js#L13

Let me know if you run into problems.


#5

Thanks @AlexSwe! I will take this out for a test drive in the next day or two! :smile:


#6

Hi @AlexSwe - I have hit some snags trying out this new feature. It seems that session data from the last response in a conversation is being saved to the previous responses. Using the unit testing example, make the following changes:

Update this intent in app.js

CheckPowerUserIntent() {
        this.$session.$data.someData = 'TestData';
        const sessionsCount = this.$user.$metaData.sessionsCount;

        if (sessionsCount > 10) {
            this.ask('Hey buddy!');
        } else {
            this.ask('Hello sir!')
        }
    },

Then just paste this code over everything in sample_sendToApp.test.js

'use strict';
const { App, Util } = require('jovo-framework');
const { GoogleAssistant } = require('jovo-platform-googleassistant');
const { Alexa } = require('jovo-platform-alexa');
// jest.setTimeout(500);


for (const p of [new GoogleAssistant()]) {
    const testSuite = p.makeTestSuite();

    describe(`PLATFORM: ${p.constructor.name} INTENTS` , () => {
        test('should return a welcome message and ask for the name at "LAUNCH"', async () => {
            const conversation = testSuite.conversation({
                runtime: "app"
            });

            const launchRequest = await testSuite.requestBuilder.intent('HelloWorldIntent');
            const responseLaunchRequest = await conversation.send(launchRequest);

            const launchRequest2 = await testSuite.requestBuilder.intent('CheckPowerUserIntent');
            const responseLaunchRequest2 = await conversation.send(launchRequest2);

            console.log('------------------------------------');
            console.log(responseLaunchRequest.outputContexts);
            console.log('------------------------------------');

            console.log('------------------------------------');
            console.log(responseLaunchRequest2.outputContexts);
            console.log('------------------------------------');

            expect(
                responseLaunchRequest.isAsk('Hello World! What\'s your name?', 'Please tell me your name.')
            ).toBe(true);

        });
    });
}

Next, run the test and you should see on the log that the someData attribute is part of the session data for the first response, when it should not be.

Take out the runtime: "app" option from the conversation and this does not occur. Also, this appears to be google only, which is why its not in the sample above.

Very odd indeed, thoughts?


#7

Hi @AlexSwe! Just wondering if you have had a chance to look at the above issue? I updated things today and was still seeing the issue.