Error in cypress object model : Cannot find module '../ObjectModel/LoginPage' - javascript

I tried to write object model in vscode, to create an object.
This is login2 :
/// <reference types="Cypress" />
import LoginPage from "../../integration/ObjectModel/LoginPage"
describe('home page', () => {
it('loginPage', function () {
const lp = new LoginPage()
})
})
This is object page :
class LoginPage {
visit(){
cy.visit('https://facebook.com');
}
fillEmail(value){
const feild = cy.get('#email')
feild.clear()
feild.type(value)
return this
}
fillPassword(value){
const feild = cy.get('#pass')
feild.clear()
feild.type(value)
return this
}
submit(){
const button = cy.get('#u_0_b')
button.click()
}
}
export default LoginPage;
This is the path of files:
and this the error
How I can solve it?
I tried to change the path as I read, also it does not solved.

Seems like the file login2.js is in the same directory as ObjectModel. Thus you only need to import file like this:
import LoginPage from "./ObjectModel/LoginPage"

You are trying to use the import/export, which is newer/modern version to module.exports and it works only when you install babel and setup the babel configuration to transform the modules in order to work with import/export. Please try to replace export default LoginPage with module.exports = new LoginPage(); in the page class.
const LoginPage = require("../../integration/ObjectModel/LoginPage")
And in your it/test, you don't need to say const lp = new LoginPage() as you are importing the page on top with require

the problem solved by changing the path to
import LoginPage from "./ObjectModel/LoginPage"

Related

Multiple exports from react.js file

I have a react.js file MyComponent that has this at the end:
module.exports = MyComponent, where MyComponent is a function defined in the file.
What if I want to export ANOTHER function: MyHelperComponent from the same file, so that another react component from another react.js file may use MyHelperComponent directly ?
So my question is: how do I export a function that is not the 'main' component of the module ?
You can only export one value from a module.
If you need to use multiple values outside it, then you need to group them somehow. Typically you would put them in an object and then export that object.
module.exports = { MyComponent, MyHelperComponent };
And then:
const MyComponent = require("./mymodule.js").MyComponent;
const MyHelperComponent = require("./mymodule.js").MyHelperComponent;
or
const mymodule = require("./mymodule.js")
const MyComponent = mymodule.MyComponent;
const MyHelperComponent = mymodule.MyHelperComponent;
or
const {MyComponent, MyHelperComponent} = require("./mymodule.js");
That said, it is usual to structure code on a one-component-per-module basis, so you might want to rethink doing this in the first place.
You can do it like this:
//...function defs here
MyComponent.helper = MyHelperComponent
module.exports = MyComponent
And then just access MyComponent.helper(args) to call the helper function.
You can export like this: export { component1, component2 }
And use like this: import { component1, component2 } from './url'
or
import * as components from './url'
<components.component1 />
Hope helpful...

TypeError: Webpack imported module is not a function

I have a backend that calculates work shifts.
I am trying to post some required user input with a module in services/shifts.
The getAll method works fine, but posting throws an error
TypeError: _services_shifts__WEBPACK_IMPORTED_MODULE_2__.default.postData is not a function
Shiftservice module:
import axios from 'axios'
const baseUrl = '...'
const getAll = () => {
const request = axios.get(baseUrl)
return request.then(response => response.data)
}
const postData = newObject => {
const request = axios.post(baseUrl, newObject)
return request.then(response => response.data)
}
export default {getAll, postData}
I have a button that triggers the following calling code on click:
import shiftService from './services/shifts'
const postData = (event) => {
event.preventDefault()
const sampleObject = {
sampleField: sample
}
shiftService
.postData(sampleObject)
.then(returnedData => {
console.log(returnedData)
})
}
When execution reaches shiftService.postData, the error is thrown.
I am really confused since I am basically copying some older project of mine which works, but here I just don't find the problem. Thank you in advance for helping a newcomer!
Modules provide special export default (“the default export”) syntax to make the “one thing per module” way look better.There may be only one export default per file.And we may neglect the name of the class in the following example.
//Module1
export default class{
}
And then import it without curly braces with any name:
//Module2
import anyname from './Module1'
Your scenario is different having two functions.You can either export default one function
export default getAll
and normal export the other function.
export postData
and when importing
import{ default as getAll,postData} from './yourModule'
OR
Remove default here in Shiftservice module and export normally:
import axios from 'axios'
const baseUrl = '...'
const getAll = () => {
const request = axios.get(baseUrl)
return request.then(response => response.data)
}
const postData = newObject => {
const request = axios.post(baseUrl, newObject)
return request.then(response => response.data)
}
export {getAll, postData}
Importing in your module
import {getAll,PostData} from './Module1'
or
import * as shiftService from './Module1'
and then use shiftServer.postData().....
Okay, I am embarrassed of the solution. I was just editing an earlier version of shiftService from a wrong folder, and the imported service only had the get method in it...
So my code actually works if placed correctly. Thank you for your time, and thanks for sharing alternative ways that must work aswell.
I think its becuse your declaring the function as arrow functions
and export it this way:
export default {getAll, postData}
you need to declare them as a normal functions
function postData(){
}
that should work

Export function from React Class

I am trying to implement a dapp using react js.In my app.js inside the app class, there is a method that I want to export to another module.I tried many options,but none of them worked for me. In the below code, I want to export username().
App.js Code
export default class SessionStore
{
......
username ()
{
if (!this._user || !this._user.username) return null
return this._user.username
}
......
}
How to import and use username() function to another modules. I am new to reactjs, so please help me to resolve this issue.
You can make the method static, but can't access props or state in the method. Better write a util function, which accepts user as argument and import in both classes.
utils.js
export const getUserName = (user = {}) => user.username ? user.username : null;
App.js
import { getUserName } from '../utils';
export default class SessionStore {
......
username = () => getUserName(this._user);
......
}

Javascript export default and import

I have the following code:
const API1 = new API({
...
})
const API2 = new API({
...
})
export default { API1, API2 }
I need to import like this:
import API1 from '/lib/api'
API1.get()...
But it doesn't work.
I don't want to do this:
import blah from '/lib/api'
blah.API1.get()...
How can I solve this ?
Thanks.
If you need to export multiple items, and don't want to have to create two variables in the consuming module (one for the default import - the object, and another for the API1 property), your only other option is to change the default export to a named export, allowing you to import just one particular named property:
const API1 = new API({
...
})
const API2 = new API({
...
})
export { API1, API2 }
and
import { API1 } from '/lib/api'
API1.get()...
The export { syntax indicates that the export is named, rather than default, and the import { syntax indicates that you're importing a named import, rather than a default import.
(It looks a lot like destructuring, and it's a little bit similar, but it's not the same)
Since you're default exporting an object you need to access individual property to access there methods, Instead you can use named exports
// exporting values
export const API1 = new API({
...
})
export const API2 = new API({
...
})
// Importing values
import { API1 } from '/lib/api'
API1.get()...

How to Export Variables with a Dynamic Names

I have a components folder in nuxt.js
/components/atoms/
and inside that folder I have an index.js to export all components dynamically
const req = require.context('./', true, /\.vue$/)
const components = {}
req.keys().forEach(fileName => {
const componentName = fileName.replace(/^.+\/([^/]+)\.vue/, '$1')
components[componentName] = req(fileName).default
})
export const { ButtonStyled, TextLead, InputSearch } = components
so I can import perfectly as I wish
import { ButtonStyled } from "#/components/atoms"
the problem is that I am defining the variables to be exported statically, fixed, so for each created component I would need to add another variable manually
I need to dynamically export the variable name
Example:
DynamicCreation = ['ButtonStyled', 'TextLead', 'InputSearch']
export const { DynamicCreation } = components
// output -> export const { ButtonStyled, TextLead,InputSearch } = components
I need to export the name of already unstructured variables
Note: I can not use this export default components because I can not import like this import { ButtonStyled } from "#/components/atoms"
You should be able to do it like this:
export default components
Then in your file where you want to use the components:
import * as components from '#/components/atoms'
Then when you need to use the components in your Vue files, you need to map them:
#Component({
components: {
ButtonStyled: components.ButtonStyled
}
})
And now you have:
<ButtonStyled></ButtonStyled>
You can make something like this way, check if is what do you need.
Create a file to import a conjunct of components: allComponents.js
export default {
componentOne: require('./passToOneComponent.js');
componentTwo: require('./passToOneComponent.js');
componentThree: require('./passToOneComponent.js');
}
After in index.js export the allComponents.js with the name that you wish:
export {default as SomeName } from 'allComponents.js';
So in the final file, you can make something like:
import { SomeName } from 'index.js';
SomeName.componentOne();
I created a library that does this type of export, anyone who wants can install via npm
I created a Webpack Plugin that makes named exports from a component, maybe this helps other people
Weback Plugin - named-exports

Categories

Resources