Cypress error Cypress.moment.duration (moment is not defined) - cypress

I'm using Cypress and upgraded to version to v8.3.1 and a new error keeps showing up.
Cannot read property 'duration' of undefined
Because this error occurred during a after all hook we are skipping all of the remaining tests.
Location: node_modules/#cypress/code-coverage/support.jsat line210
cy.task('coverageReport', null, {
timeout: Cypress.moment.duration(3, 'minutes').asMilliseconds(),
^
log: false
})
It says that duration cannot be found since Cypress.moment doesn't exist.
I checked the changelog and they removed it:
Cypress.moment() has been removed. Please migrate to a different datetime formatter. See our recipe for example replacements. Addresses #8714.
But since I'm not directly using it, it's in the code coverage included in Cypress, I don't know how to fix it.

Somehow you've obtained an old version of #cypress/code-coverage.
Perhaps you upgraded Cypress and not the code-coverage package?
#cypress/code-coverage#3.2.0 - support.js
after(function generateReport() {
// when all tests finish, lets generate the coverage report
cy.task('coverageReport', {
timeout: Cypress.moment.duration(3, 'minutes').asMilliseconds()
})
})
#cypress/code-coverage#3.9.10 - support.js
after(function generateReport() {
...
cy.task('coverageReport', null, {
timeout: dayjs.duration(3, 'minutes').asMilliseconds(),
log: false
}).then((coverageReportFolder) => {
...
})
npm update #cypress/code-coverage should fix it

Related

cy.task gives an error but no explanation, how can I fix it?

I'm using Cypress for frontend testing and I defined a task in cypress.config file.
setupNodeEvents(on) {
on('task', {
downloads: downloadspath => {
return fs.readdirSync(downloadspath);
},
deleteFiles: directory => {
const files = fs.readdirSync(directory);
if (files.length === 0) {
return 0;
}
for (const file of files) {
fs.unlinkSync(path.join(directory, file));
}
return 0;
},
});
},
And when I use it in my tests, it gives an error like this:
There's no explanation so, I don't know how to fix this error.
cy.task does a simple work, it just deletes all files in the cypress/downloads folder.
Generally, if the task throws an error it shows up in the Cypress log. You may get more info if you open devtools and click on the Cypress log entry.
Specifically, fs.unlinkSync(path.join(directory, file)) is probably failing because there is a sub-directory in the directory, see Node.js fs.unlinkSync() Method
The fs.unlinkSync() method is used to synchronously remove a file or symbolic link from the filesystem. This function does not work on directories, therefore it is recommended to use fs.rmdir() to remove a directory.
Since you are deleting cypress/downloads in the test, be aware Cypress does that for you
Cypress Configuration - Downloads
trashAssetsBeforeRuns (default: true)
Whether Cypress will trash assets within the downloadsFolder, screenshotsFolder, and videosFolder before tests run with cypress run.

GraphQL Nexus Schema (nexusjs) doesn't compile with scalar types

I am trying to follow the documentation on the Nexus-Schema (nexusjs) website for adding scalar types to my GraphQL application.
I have tried adding many of the different implementations to my src/types/Types.ts file using the samples provided in the documentation and the interactive examples. My attempts include:
Without a 3rd party libraries:
const DateScalar = scalarType({
name: 'Date',
asNexusMethod: 'date',
description: 'Date custom scalar type',
parseValue(value) {
return new Date(value)
},
serialize(value) {
return value.getTime()
},
parseLiteral(ast) {
if (ast.kind === Kind.INT) {
return new Date(ast.value)
}
return null
},
})
With graphql-iso-date 3rd party library:
import { GraphQLDate } from 'graphql-iso-date'
export const DateTime = GraphQLDate
With graphql-scalars 3rd party library (as shown in the ghost example):
export const GQLDate = decorateType(GraphQLDate, {
rootTyping: 'Date',
asNexusMethod: 'date',
})
I am using this new scalar type in an object definition like the following:
const SomeObject = objectType({
name: 'SomeObject',
definition(t) {
t.date('createdAt') // t.date() is supposed to be available because of `asNexusMethod`
},
})
In all cases, these types are exported from the types file and imported into the makeSchema's types property.
import * as types from './types/Types'
console.log("Found types", types)
export const apollo = new ApolloServer({
schema: makeSchema({
types,
...
context:()=>(
...
})
})
The console.log statement above does show that consts declared in the types file are in scope:
Found types {
GQLDate: Date,
...
}
If I run the app in development mode, everything boots up and runs fine.
ts-node-dev --transpile-only ./src/app.ts
However, I encounter errors whenever I try to compile the app to deploy to a server
ts-node ./src/app.ts && tsc
Note: This error occurs occurs running just ts-node ./src/app.ts before it gets to tsc
The errors that shown during the build process are the following:
/Users/user/checkouts/project/node_modules/ts-node/src/index.ts:500
return new TSError(diagnosticText, diagnosticCodes)
^
TSError: тип Unable to compile TypeScript:
src/types/SomeObject.ts:11:7 - error TS2339: Property 'date' does not exist on type 'ObjectDefinitionBlock<"SomeObject">'.
11 t.date('createdAt')
Does anyone have any ideas on either:
a) How can I work around this error? While long-term solutions are ideal, temporary solutions would also be appreciated.
b) Any steps I could follow to debug this error? Or ideas on how get additional information to assist with debugging?
Any assistance would be very much welcomed. Thanks!
The issue seems to be resolved when --transpile-only flag is added to the nexus:reflect command.
This means the reflection command gets updated to:
ts-node --transpile-only ./src/app.ts
and the build comand gets updated to:
env-cmd -f ./config/.env ts-node --transpile-only ./src/app.ts --nexusTypegen && tsc
A github issue has also been created which can be reviewed here: https://github.com/graphql-nexus/schema/issues/690

Is there a way to add cypress test steps execution information into allure report?

I'm trying to add test step information into an allure report, i use cypress and generate report with allure, report are correctly generate but no details appears in testcases.
I try to use the on('task') without success...
my plugins/index.js contain:
...
on('task', {
allureTestStep () {
const testStep = allure.createStep("Initial", () => {
console.log("First Test")
});
testStep()
return null
}
})
...
and i call it with:
cy.task('allureTestStep')
in my test.
No log in console only two error:
allure-js-commons: Unexpected startStep() of initial. There is no parent step
First Test
allure-js-commons: Unexpected endStep(). There are no any steps running
and in the report nothing is displayed(no error, no step detail).
Any help is welcome :)
I'm using the cypress-allure plugin and you can set it to show cypress commands in the test results. If you call cy.log those also get placed there. It's pretty nice.
it(`Should have title of ${treeListTitle}`, () => {
cy.log('title should be set');
...
}
Notice in the allure results where that is printed out.

Cannot read property 'Base' of undefined error using mocha-allure-reporter in Cypress

I am trying to use mocha-allure-reporter in Cypress. I installed mocha and mocha-allure-reporter as dev dependencies and mentioned mocha-allure-reporter as the reporter in cypress.json.
I tried the below code quoted in the example section of mocha allure page:
require('mocha-allure-reporter');
describe("simple test demo", () => {
const testStep = allure.createStep("initial", () => {
console.log("First Test")
});
it("simple passed test", () => {
testStep();
});
}
However, I am getting the following error:
Uncaught TypeError: Cannot read property 'Base' of undefined
...at the first line itself:
require('mocha-allure-reporter')
Upon looking on console, I see that the error is originated at line - var Base = require("mocha").reporters.Base in the Allure reporter :
var Base = require("mocha").reporters.Base;
var Allure = require("allure-js-commons");
...
...
global.allure = new Runtime(allureReporter);
/**
* Initialize a new `Allure` test reporter.
*
* #param {Runner} runner
* #param {Object} opts mocha options
* #api public
*/
function AllureReporter(runner, opts) {
...
...
Please note that the following output xml file is getting created in the allure-results directory once the execution is done.
<?xml version='1.0'?>
<ns2:test-suite xmlns:ns2='urn:model.allure.qatools.yandex.ru' start='1547481439243' stop='1547481439477'>
<name></name>
<title></title>
<test-cases>
<test-case start='1547481439282' status='broken' stop='1547481439460'>
<name>An uncaught error was detected outside of a test</name>
<title>An uncaught error was detected outside of a test</title>
<labels/>
<parameters/>
<steps/>
<attachments/>
<failure>
<message>Cannot read property 'Base' of undefined
This error originated from your test code, not from Cypress.
When Cypress detects uncaught errors originating from your test code it will automatically fail the current test.
Cypress could not associate this error to any specific test.
We dynamically generated a new test to display this failure.</message>
<stack-trace>Uncaught TypeError: Cannot read property 'Base' of undefined
This error originated from your test code, not from Cypress.
When Cypress detects uncaught errors originating from your test code it will automatically fail the current test.
Cypress could not associate this error to any specific test.
We dynamically generated a new test to display this failure.
at Object.<anonymous> (http://localhost:61925/__cypress/tests?p=cypress\integration\Tests\Test.spec.js-289:15125:38)
at Object.98.allure-js-commons (http://localhost:61925/__cypress/tests?p=cypress\integration\Tests\Test.spec.js-289:15201:4)
at o (http://localhost:61925/__cypress/tests?p=cypress\integration\Tests\Test.spec.js-289:1:265)
at http://localhost:61925/__cypress/tests?p=cypress\integration\Tests\Test.spec.js-289:1:316
at Object.40.mocha-allure-reporter (http://localhost:61925/__cypress/tests?p=cypress\integration\Tests\Test.spec.js-289:7566:1)
at o (http://localhost:61925/__cypress/tests?p=cypress\integration\Tests\Test.spec.js-289:1:265)
at r (http://localhost:61925/__cypress/tests?p=cypress\integration\Tests\Test.spec.js-289:1:431)
at http://localhost:61925/__cypress/tests?p=cypress\integration\Tests\Test.spec.js-289:1:460</stack-trace>
</failure>
</test-case>
</test-cases>
</ns2:test-suite>
Please guide me. Thanks!
I was able to use mocha-allure-reporter by simply installing it (along with mocha),
npm install mocha mocha-allure-reporter
and setting up a script in package.json, following the Cypress guidelines for npm reporters here
"scripts": {
...
"cypress:run": "cypress run --reporter mocha-allure-reporter"
Note, I think that these reporters only work with the Cypress 'run' command, not the Cypress 'open' command.
The output is a folder called 'allure-results' which contains a bunch of xml files. I presume these can then be displayed using the Allure framework tool.
Example output file:
<?xml version='1.0'?>
<ns2:test-suite xmlns:ns2='urn:model.allure.qatools.yandex.ru' start='1547254197911' stop='1547254201289'>
<name>Tasks Page</name>
<title>Tasks Page</title>
<test-cases>
<test-case start='1547254199721' status='passed' stop='1547254199815'>
<name>should have a title</name>
<title>should have a title</title>
<labels/>
<parameters/>
<steps/>
<attachments/>
</test-case>
</test-cases>
</ns2:test-suite>
Run allure code in cy.task()
To run the allure code, you need to access the nodejs context through cy.task.
For example,
/cypress/plugins/index.js
require('mocha-allure-reporter');
module.exports = (on) => {
on('task', {
allureTestStep () {
const testStep = allure.createStep("initial", () => {
console.log("First Test")
});
testStep()
return null
}
})
}
spec
describe("simple test demo", () => {
it("simple passed test", () => {
cy.task('allureTestStep')
});
})
Note this produces the console log in the command window where you start Cypress, not the browser console.
However, you can pass a value back from the task into the test, depending on what you are actually trying to do (see docs for details).

Find a good way to get a coverage report on ES6 code

Here is my problem : in an AngularJS project, I use Babel 6 (ES2015 + stage3 + decorators), SystemJS and Karma + Jasmine.
All is OK, except for code coverage : I can't find a good way to get a coverage report on ES6 code instead of transpiled ES5.
I have some restrictions :
no Github npm dependencies
no deprecated projects (like isparta)
should run on Windows (yeah, I know, but I have to) and GNU/Linux
This question is especially oriented to a local html coverage report, but I also need a format that could be used in SonarQube.
There seems to be a huge lake of support on this question (especially with Decorators and SystemJS), so, along with some issues on Github, maybe the stackoverflow community could share a way to do this.
If you need some extras, you can see this project on Github (Sedona-Solutions/sdn-angularjs-seed), and look at the related issues (Sedona-Solutions/sdn-angularjs-seed#5) with references to all related issues and projects I could found.
About remap-istanbul
Here is my transpilation and mapping gulp task :
gulp.task('es6', function () {
return gulp.src(paths.source, { base: 'app' })
.pipe(plumber())
.pipe(changed(paths.output, { extension: '.js' }))
.pipe(sourcemaps.init())
.pipe(babel(compilerOptions))
.pipe(ngAnnotate({
sourceMap: true,
gulpWarnings: false
}))
.pipe(sourcemaps.write("."))
.pipe(gulp.dest(paths.output))
});
Karma-coverage
coverageReporter: {
reporters: [
{
type: 'json',
dir: 'target/coverage/',
subdir: '.',
file: 'coverage-final.json'
}
]
}
and the test:remap gulp task :
gulp.task('test:remap', ['test'], function (done) {
return gulp.src(paths.coverage + '/coverage-final.json')
.pipe(remapIstanbul({
reports: {
'json': paths.coverage + '/coverage.json',
'html': paths.coverage + '/html-remaped/'
},
fail: true,
useAbsolutePaths: true
}));
});
due to the first remap-istanbul issue, I get the following error :
C:\...\sdn-angularjs-seed\node_modules\remap-istanbul\node_modules\istanbul\lib\report\html.js:288
text = structuredText[startLine].text;
^
TypeError: Cannot read property 'text' of undefined
at C:\...\sdn-angularjs-seed\node_modules\remap-istanbul\node_modules\istanbul\lib\report\html.js:288:53
You can see the corresponding code on github.
It could be an error on some mapping generation, or paths ... If you think so, tell me.
karma-coverage + remap-istanbul should work fine

Resources