Actor picture

Actor Testing

pocesar/actor-testing

Test your actors with varying inputs and expected outputs, duplicates, bad output fields, or unexpected log messages using Jasmine

No credit card required

Author's avatarPaulo Cesar
  • Modified
  • Users9
  • Runs9,914

To run the code examples, you need to have an Apify account. Replace <YOUR_API_TOKEN> in the code with your API token. For a more detailed explanation, please read about running actors via the API in Apify Docs.

const { ApifyClient } = require('apify-client');

// Initialize the ApifyClient with API token
const client = new ApifyClient({
    token: '<YOUR_API_TOKEN>',
});

// Prepare actor input
const input = {
    "testSpec": ({ it, xit, moment, _, run, expect, expectAsync, input, describe }) => {
        (input.resource ? [
            'beta',
        ] : [
            'latest',
        ]).forEach((build) => {
            describe(`${build} version`, () => {
                it('test something-task', async () => {
                    const runResult = await run({
                        taskId: '',
                    });
    
                    await expectAsync(runResult).toHaveStatus('SUCCEEDED');
                    await expectAsync(runResult).withLog((log) => {
                        expect(log)
                            .withContext(runResult.format('ReferenceError'))
                            .not.toContain('ReferenceError');
                        expect(log)
                            .withContext(runResult.format('TypeError'))
                            .not.toContain('TypeError');
                    });
    
                    await expectAsync(runResult).withStatistics((stats) => {
                        expect(stats.requestsRetries)
                            .withContext(runResult.format('Request retries'))
                            .toBeLessThan(3);
    
                        expect(stats.crawlerRuntimeMillis)
                            .withContext(runResult.format('Run time'))
                            .toBeWithinRange(0.1 * 60000, 10 * 60000);
                    });
    
                    await expectAsync(runResult).withDataset(({ dataset, info }) => {
                        expect(info.cleanItemCount)
                            .withContext(runResult.format('Dataset cleanItemCount'))
                            .toBeGreaterThan(0);
    
                        expect(dataset.items)
                            .withContext(runResult.format('Dataset items array'))
                            .toBeNonEmptyArray();
                    });
                });
            });
        });
    },
    "slackChannel": "#public-actors-tests-notifications"
};

(async () => {
    // Run the actor and wait for it to finish
    const run = await client.actor("pocesar/actor-testing").call(input);

    // Fetch and print actor results from the run's dataset (if any)
    console.log('Results from dataset');
    const { items } = await client.dataset(run.defaultDatasetId).listItems();
    items.forEach((item) => {
        console.dir(item);
    });
})();