我正在为反应代码库添加打字稿支持,虽然应用程序工作正常,但是jest测试在整个地方都失败了,显然没有认识到有关es6语法的东西.
我们正在使用ts-jest.下面是我正在尝试处理jest的测试设置文件时的错误消息.
FAIL src/data/reducers/reducers.test.js
? Test suite failed to run
/Users/ernesto/code/app/react/setupTests.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import './polyfills';
^^^^^^^^^^^^^
SyntaxError: Unexpected string
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)
Run Code Online (Sandbox Code Playgroud)
它无法识别一个简单的import './polyfills',说引用的字符串是意外的.
这些是我的设置:
package.json中的jest配置
"jest": {
"setupTestFrameworkScriptFile": "<rootDir>/app/react/setupTests.js",
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
]
},
Run Code Online (Sandbox Code Playgroud)
tsconfig.json
{
"compilerOptions": {
"declaration": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": ["es6", "dom"],
"module": "es6",
"moduleResolution": "node",
"allowJs": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"target": "es5",
"jsx": "react",
"forceConsistentCasingInFileNames": true,
"noImplicitReturns": true, …Run Code Online (Sandbox Code Playgroud) 我在Swift上为owner/ownee方案建模:
class Owner<T: Ownee> {
// ...
}
protocol Ownee {
var owner: Owner<Self> { get }
}
Run Code Online (Sandbox Code Playgroud)
然后我有一对班级教授/学生遵守上面的建模类型:
class Professor: Owner<Student> {
// ...
}
class Student: Ownee {
let professor: Professor
var owner: Owner<Student> { // error here (see below)
return professor
}
init(professor: Professor) {
self.professor = professor
}
}
Run Code Online (Sandbox Code Playgroud)
但是我var owner在Student类中的定义上得到以下错误:
协议'Ownee'要求'所有者'不能被非最终类('学生')满足,因为它在非参数非结果类型位置使用'Self'
我试图理解这个错误的原因是什么,为什么让Student最终的类会修复它,如果有一些解决方法能够以不同的方式对它进行建模,而不是让这个类最终.我已经搜索了这个错误,但到目前为止还没有找到太多.
我的问题是在为API目的构建URL时嵌套资源的优势.考虑以下两种用于访问员工资源的备选方案:
/api/employees?department=1 # flat
Vs.
/api/departments/1/employees # nested
Run Code Online (Sandbox Code Playgroud)
现在考虑开发通用库以从API访问REST资源的任务.如果所有路由都是平坦的,那么这样的REST包装器库只需要知道被访问资源的名称:
store.query('employees', {department_id:1}) => /api/employees?department=1
Run Code Online (Sandbox Code Playgroud)
但是,如果我们要支持嵌套路由,那么这个包装器需要知道有关嵌套模型和其他资源的额外信息,以便了解如何构建用于引用此类模型的URL.鉴于并非所有模型都嵌套在相同的父资源下,甚至一些模型根本不会嵌套,REST包装器库需要具有某种配置来描述所有这些额外的知识,否则这些知识是不需要的.
所以我的问题是:
API中的嵌套资源路由是否有任何实际优势?(这并不意味着最终用户使用,因此拥有更漂亮的URL可以获得更少的收益).
嵌套方法是否真的比平面更好,超越美学,以便证明为支持资源URL构建缺乏统一性而引入的额外努力和复杂性?
另见:https://stackoverflow.com/a/36410780/621809
更新:重要的澄清
我从一些评论和答案中了解到,我对一个方面不够清楚:我并不反对使用/employees/5或等URL来处理单个资源/departments/1.我不认为这是嵌套的.
当我说嵌套资源时,我指的是像/departments/1/employees资源一直在另一个资源的上下文中寻址的URL .主要问题是,对于URL构建,通用库需要知道额外的东西,如"员工嵌套在部门下",但"分支不嵌套在任何东西下".如果所有资源都可以通过REST来解决,但是以平面方式解决,知道如何解决它们会更简单,更容易预测.
当您考虑它时,在数据库中,您不需要知道额外的信息,以便知道如何处理对象集合(例如RDMS中的表).您总是将员工集合称为employees,而不是departments/5/employees.
我正在编写一个.sh脚本,在某些时候,会将用户的shell更改为/ bin/zsh.该命令当然如下:
chsh -s /bin/zsh
Run Code Online (Sandbox Code Playgroud)
但是这会询问用户的密码,我想只在当前用户的shell尚未执行时才执行它/bin/zsh.为此,我需要一个让我知道当前shell的命令,并将其与"/bin/zsh"类似的东西进行比较.我发现有ac getusershell函数,但是有没有办法从shell脚本中知道这个?
更新:对不起,我指的是用户指定为他首选shell的shell.所以,是的,指定的那个/etc/passwd.这背后的逻辑是,脚本即将将用户首选的shell更改为zsh,我只想让脚本首先检查它是否已经是zsh.
Ember CLI应用程序有一个package.json,它将所有内容列为dev依赖项.甚至是应用程序生产版本中需要的东西.例如,ember和ember-data等软件包作为devdependencies安装.
作为参考,这里是我正在谈论的样本:https://github.com/ember-cli/ember-new-output/blob/master/package.json#L17-L38
这是什么原因?
有一些material-ui组件无法将其结果与父组件放置在同一位置。其中,我们有Dialog,Menu等等。
显然,这不可能在装有某些父组件的jest.js包装器中测试其内容是否存在。
例如,给定以下组件:
class DropdownMenu extends React.Component {
onButtonClick = (e) => {
this.setState({ open: true, anchorEl: e.currentTarget });
}
render() {
return (
<div>
<Button onClick={this.onButtonClick}>Menu</Button>
<Menu
open={this.state.open}
onRequestClose={() => this.setState({ open: false })}
>
<MenuItem label="Home" />
<MenuItem label="Sign in" />
</Menu>
</div>
);
}
}
Run Code Online (Sandbox Code Playgroud)
即使此测试应直观地起作用,它也会失败:
it('renders some menu items', () => {
const wrapper = mount(<AppMenu />);
expect(wrapper).toContainReact(<MenuItem label="Home" />);
});
Run Code Online (Sandbox Code Playgroud)
这是杰斯特失败的输出:
renders some menu items
Expected <AppMenu> to contain <withStyles(MenuItem) className="MenuItem" …Run Code Online (Sandbox Code Playgroud) 我一直在阅读有关自定义操作的rails_admin wiki部分,但我对rails_admin完全不熟悉,这个文档对我来说有点混乱.
我需要实现的是,当管理员用户点击删除特定员工的选项(员工是我的应用程序中的模型)时,实际删除给定员工的代码不能是rails_admin删除的默认方式,而是我需要以某种方式提供给rails_admin的给定代码块.
请注意,我仍然希望通过rails_admin访问的其余数据模型以通常的方式删除.只有员工模型需要具有自定义删除例程.
我正在研究一个应用程序,我们正在研究使用jsonapi来描述所有API响应中的数据的可能性.它对于类似资源的实体非常有效,但我们在提出一种以类似jsonapi的方式描述报告数据的方法时遇到了一些麻烦.
通过报告数据,我指的是从我们存储在数据库中的基本资源类数据中即时聚合和计算的数据.例如,想象一下我们存储房地产信息,我们有关于房屋,公寓和办公空间的信息,每个都与位置,房屋面积(平方英尺),房产类型(房屋,公寓或办公空间)相关联,以及有关房产的任何其他相关信息.现在想象一下,我们需要一个接收的报告,?group_by[]=location&group_by[]=type我们希望响应能够传达有关这两个group_by参数交集的汇总信息.因此,我们将收到一个对象,其中包含给定位置中所有属性的平均平方英尺面积,也按属性类型分组.
Average Property Sizes (in square feet)
Houses Apartments Offices
Manhattan 1234.56 234.56 123.45
Cape Coral 456.78 654.32 876.54
Portland 4321.00 987.65 2345.67
Run Code Online (Sandbox Code Playgroud)
我们可以从这些数据中想到的最类似资源的是每个单元格,但由于它们是更多基本数据的计算聚合的结果,因此它们没有自然ID.我们一直在思考与计算ID提交他们以及(也许结合通过它们的数据分组的尺寸,即的ID "house,34",其中house代表一种类型的属性,34是位置"曼哈顿"的ID).然后,每个单元将与相应的位置记录具有关系,该记录将包括在included有效载荷的部分中.这是一个示例json文件的示例:
{
"data": [
{
"id": "house,123",
"type": "report_items",
"attributes": {
"property_type": "house",
"value": 108.75
},
"relationships": {
"location": {
"data": {
"type": "locations",
"id": 123
}
}
}
},
{
"id": "house,124",
"type": "report_items",
"attributes": {
"property_type": "house",
"value": 36.0
},
"relationships": { …Run Code Online (Sandbox Code Playgroud) 我在Ember应用程序中运行测试时遇到此错误.显然,语法错误(Unexpected token ',')的发生方式使得测试环境的负载失败,因此它会使每一次测试都失败.
$ ember test
version: 1.13.13
Built project successfully. Stored in "/Users/ernesto/code/projects/my-app/frontend/tmp/class-tests_dist-cqvfx9XF.tmp".
not ok 1 PhantomJS 2.0 - global failure
---
actual: >
null
message: >
SyntaxError: Unexpected token ','
Log: |
...
not ok 2 PhantomJS 2.0 - global failure
---
actual: >
null
message: >
Error: Could not find module `frontend/config/environment` imported from `frontend/tests/helpers/resolver`
Log: |
...
not ok 3 PhantomJS 2.0 - global failure
---
actual: >
null
message: >
Error: Assertion Failed: …Run Code Online (Sandbox Code Playgroud) 最初的问题并非直接与赛普拉斯缺乏对fetch api的支持有关,但这是我最初在此处发布问题的原因。我已经编辑了问题的标题,并在此处添加了此序言。本段下面是问题的原始内容:
赛普拉斯文档中包含许多示例,您可以在其中为特定的Web请求设置别名,然后可以指示赛普拉斯等待它。例如,这一个:
cy.route('POST', '/login').as('postLogin')
cy.get('input[name=username]').type('jane.lae')
cy.get('input[name=password]').type('password123{enter}')
// we should always explicitly wait for
// the response for this POST to come back
// so our tests are not potentially flaky or brittle
cy.wait('@postLogin')
Run Code Online (Sandbox Code Playgroud)
问题是我正在尝试在我的应用程序的测试中使用这种确切的技术,但我需要等待的请求不是针对托管该应用程序的同一服务器发出的。因此,我假设我可以输入后端端点的完整URL,因此cy.route不会在前面加上baseUrl,它是托管前端应用程序的主机。
// The following URL points to a backend rails app
// The frontend app I'm testing with cypress is at http://localhost:8080
cy.route('POST', 'http:/localhost:3000/session').as('postLogin')
// ... fill login form
cy.wait('@postLogin')
// The test never reaches this …Run Code Online (Sandbox Code Playgroud) 想象一个更传统的 Web 应用程序,它正在一个复杂到足以需要它的页面中慢慢引入 React 组件。
我们正在使用 Webpack 开发这个新的 UI,并且我们使用 css 模块来实现新 UI 所需的所有新 css。但我们仍然需要重用一些全局的 css,并通过 html 中链接的传统 css 样式表在应用程序中提供。
因此,我们需要在 css 模块中偶尔编写引用全局命名空间中的 css 类的规则,而不是本地范围内的类,因此在应用程序运行时转换为更像 gem 的长 css 类名:
.container {
// rules for container
}
.container.pull-right {
// rules for when the container element also has the pull-right class
}
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,container它是一个 css 类,css 模块将使用该模块将其上下文化到特定组件。
但是pull-right是存在于页面中可用的 css 文件中的类,但不被 css 模块或 Webpack 处理。因此,我们需要一种方法(可能是语法)来告诉 css 模块“嘿,保持pull-right输出中的原样。不要弄乱它或尝试将其删除”。
我想这样的东西应该存在,但到目前为止我还没有通过谷歌搜索找到它。
我正在尝试使用ElasticSearch执行搜索,该搜索必须匹配部分索引中的某些搜索项,但仅适用于属于某些项目的部分,即当前用户可以访问的项目.
所以我试图将一个过滤器应用于查询,以便它过滤那些将project_id包含在一小组2或3个不同值中的部分.如果某个部分的project_id属于有效的project_id,则应该包含它.否则它不会.
这都是使用elasticsearch-model gem的rails api的上下文.
这是我传入的查询.它假装过滤部分,因此只有项目2和5中的部分包含在结果中.
{
"query": {
"filtered": {
"query": {
"multi_match": {
"query": "search terms here",
"type": "cross_fields",
"fields": ["title^3", "contents"],
"operator": "and"
}
},
"filter": {
"or": {
"filters": [
{ "exists": { "project_id": 2 } },
{ "exists": { "project_id": 5 } }
]
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我收到这个错误:
QueryParsingException[[sections] [exists] filter does not support [project_id]]
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我想做点什么......
class Person
has_many :things, -> { where(attr: owner.attr) }
end
Run Code Online (Sandbox Code Playgroud)
也就是说,声明与范围块的has_many关联,并且在该块内部我需要访问关联所有者,即对人员模型的引用.
所以,如果我最终调用@some_person.things的owner在范围块的上方将一个参考@some_person.
我可以使用什么代替owner上面的代码来引用该块中的关联所有者?
orm activerecord ruby-on-rails associations rails-activerecord
api-design ×2
ember.js ×2
jestjs ×2
rest ×2
testing ×2
activerecord ×1
associations ×1
babeljs ×1
bem ×1
css ×1
css-modules ×1
cypress ×1
ember-cli ×1
end-to-end ×1
enzyme ×1
es6-modules ×1
generics ×1
javascript ×1
json ×1
json-api ×1
material-ui ×1
npm ×1
oop ×1
orm ×1
package.json ×1
phantomjs ×1
rails-admin ×1
reactjs ×1
restful-url ×1
shell ×1
swift ×1
syntax-error ×1
typescript ×1